219 Contains Duplicate II 存在重复 II
给定一个整数数组和一个整数 k,判断数组中是否存在两个不同的索引 i 和 j,使 nums [i] = nums [j],并且 i 和 j 的绝对差值最大为 k。
详见:https://leetcode.com/problems/contains-duplicate-ii/description/
Java实现:
class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
int n=nums.length;
if(n==0||nums==null){
return false;
}
Map<Integer,Integer> m=new HashMap<Integer,Integer>();
for(int i=0;i<n;++i){
if(m.containsKey(nums[i])&&(i-m.get(nums[i])<=k)){
return true;
}
m.put(nums[i],i);
}
return false;
}
}
C++实现:
class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
int size=nums.size();
if(size==0||nums.empty())
{
return false;
}
unordered_map<int,int> m;
for(int i=0;i<size;++i)
{
if(m.find(nums[i])!=m.end()&&i-m[nums[i]]<=k)
{
return true;
}
m[nums[i]]=i;
}
return false;
}
};
219 Contains Duplicate II 存在重复 II的更多相关文章
- [LeetCode] 219. Contains Duplicate II 包含重复元素 II
Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...
- 217/219. Contains Duplicate /Contains Duplicate II
原文题目: 217. Contains Duplicate 219. Contains Duplicate II 读题: 217只要找出是否有重复值, 219找出重复值,且要判断两者索引之差是否小于k ...
- 【LeetCode】217 & 219 - Contains Duplicate & Contains Duplicate II
217 - Contains Duplicate Given an array of integers, find if the array contains any duplicates. You ...
- 219. Contains Duplicate II【easy】
219. Contains Duplicate II[easy] Given an array of integers and an integer k, find out whether there ...
- 219. Contains Duplicate II - LeetCode
Question 219. Contains Duplicate II Solution 题目大意:数组中两个相同元素的坐标之差小于给定的k,返回true,否则返回false 思路:用一个map记录每 ...
- [LeetCode] 220. Contains Duplicate III 包含重复元素 III
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
- [LeetCode] Contains Duplicate III 包含重复值之三
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
- Leetcode之回溯法专题-90. 子集 II(Subsets II)
Leetcode之回溯法专题-90. 子集 II(Subsets II) 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入 ...
- Leetcode之回溯法专题-47. 全排列 II(Permutations II)
Leetcode之回溯法专题-47. 全排列 II(Permutations II) 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2] ...
随机推荐
- Domino/Xpages Bootstrap 动态生成首页功能
因为之前用户须要做个动态首页的功能,但一般用户又不熟HTML,所以最佳的方法能够使用拖动的方法来配置首页,一些主要的组件是已经帮用户的依据实际数据情况已经制作OK,用户仅仅须要简单配置就能够更改首页, ...
- Tcl学习之--列表|字典
[列表|字典] Tcl使用列表来处理各种集合,比方一个目录中的全部文件,以及一个组件的全部选项.最简单的列表就是包括由随意个空格.制表符.换行符.分隔的随意多个元素的字符串.比方: JerryAlic ...
- spring依赖注入(反转控制)
SPRING依赖注入机制(反转控制)解析 Spring能有效地组织J2EE应用各层的对象.不管是控制层的Action对象,还是业务层的 Service对象,还是持久层的DAO对象,都可在Spring的 ...
- ubuntu字符界面下显示中文和调整分辨率
1.sudo apt-get install zhcon 2.vi /etc/zhcon.conf 修改下面两行 x_resolution 1024 y_resolution 768 完成这两步后在 ...
- POJ2528 Mayor's posters —— 线段树染色 + 离散化
题目链接:https://vjudge.net/problem/POJ-2528 The citizens of Bytetown, AB, could not stand that the cand ...
- 安装linux各种桌面环境
1.安装kde ①添加 KDE SC 4.11 库 打开一个终端窗口,在终端窗口中输入如下命令: sudo add-apt-repository ppa:kubuntu-ppa/backports 回 ...
- es的forcemerge——按照天分割
归并线程配置 segment 归并的过程,需要先读取 segment,归并计算,再写一遍 segment,最后还要保证刷到磁盘.可以说,这是一个非常消耗磁盘 IO 和 CPU 的任务.所以,ES 提供 ...
- 并不对劲的bzoj2638
为了反驳很对劲的太刀流,并不对劲的片手流决定与之针锋相对. 很对劲的太刀流-> 2638: 黑白染色 Time Limit: 20 Sec Memory Limit: 256 MBSubmit ...
- CodeForces-427D:Match & Catch (后缀自动机)
Police headquarter is monitoring signal on different frequency levels. They have got two suspiciousl ...
- 【HNOI2004】 打鼹鼠
[题目链接] 点击打开链接 [算法] 动态规划 f[i]表示上一次打了第i只鼹鼠,所能打死的最多的鼹鼠数量 [代码] #include<bits/stdc++.h> using names ...