219:存在重复元素 II
This commit is contained in:
parent
64d0ab8193
commit
80aa98a602
50
src/main/java/leetcode/editor/cn/ContainsDuplicateIi.java
Normal file
50
src/main/java/leetcode/editor/cn/ContainsDuplicateIi.java
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
//给定一个整数数组和一个整数 k,判断数组中是否存在两个不同的索引 i 和 j,使得 nums [i] = nums [j],并且 i 和 j 的差的 绝对值
|
||||||
|
// 至多为 k。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 1:
|
||||||
|
//
|
||||||
|
// 输入: nums = [1,2,3,1], k = 3
|
||||||
|
//输出: true
|
||||||
|
//
|
||||||
|
// 示例 2:
|
||||||
|
//
|
||||||
|
// 输入: nums = [1,0,1,1], k = 1
|
||||||
|
//输出: true
|
||||||
|
//
|
||||||
|
// 示例 3:
|
||||||
|
//
|
||||||
|
// 输入: nums = [1,2,3,1,2,3], k = 2
|
||||||
|
//输出: false
|
||||||
|
// Related Topics 数组 哈希表 滑动窗口 👍 306 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
//219:存在重复元素 II
|
||||||
|
class ContainsDuplicateIi {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new ContainsDuplicateIi().new Solution();
|
||||||
|
}
|
||||||
|
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
public boolean containsNearbyDuplicate(int[] nums, int k) {
|
||||||
|
if (k <= 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
for (int i = 0; i < nums.length; i++) {
|
||||||
|
for (int j = i + 1; j <= Math.min(i + k, nums.length - 1); j++) {
|
||||||
|
if (nums[i] == nums[j]) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
<p>给定一个整数数组和一个整数 <em>k</em>,判断数组中是否存在两个不同的索引<em> i</em> 和<em> j</em>,使得 <strong>nums [i] = nums [j]</strong>,并且 <em>i</em> 和 <em>j</em> 的差的 <strong>绝对值</strong> 至多为 <em>k</em>。</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
|
||||||
|
<pre><strong>输入:</strong> nums = [1,2,3,1], k<em> </em>= 3
|
||||||
|
<strong>输出:</strong> true</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
|
||||||
|
<pre><strong>输入: </strong>nums = [1,0,1,1], k<em> </em>=<em> </em>1
|
||||||
|
<strong>输出:</strong> true</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 3:</strong></p>
|
||||||
|
|
||||||
|
<pre><strong>输入: </strong>nums = [1,2,3,1,2,3], k<em> </em>=<em> </em>2
|
||||||
|
<strong>输出:</strong> false</pre>
|
||||||
|
<div><div>Related Topics</div><div><li>数组</li><li>哈希表</li><li>滑动窗口</li></div></div><br><div><li>👍 306</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user