LeetCode Contains Duplicate II (判断重复元素)
题意:如果有两个相同的元素,它们之间的距离不超过k,那么返回true,否则false。
思路:用map记录每个出现过的最近的位置,扫一边序列即可。扫到一个元素就判断它在前面什么地方出现过。本题数据有点弱。
- class Solution {
- public:
- bool containsNearbyDuplicate(vector<int>& nums, int k) {
- if(nums.empty()) return false;
- unordered_map<int,int> mapp;
- nums.insert(nums.begin(),);
- for(int i=; i<nums.size(); i++){
- if(!mapp[nums[i]]) mapp[nums[i]]=i; //第一次出现
- else{
- if(i-mapp[nums[i]]<=k ) return true; //已经出现,且符合条件
- mapp[nums[i]]=i; //不符合,更新最近一个位置
- }
- }
- return false;
- }
- };
AC代码
LeetCode Contains Duplicate II (判断重复元素)的更多相关文章
- LeetCode Contains Duplicate (判断重复元素)
题意: 如果所给序列的元素不是唯一的,则返回true,否则false. 思路: 哈希map解决. class Solution { public: bool containsDuplicate(vec ...
- [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 ...
- [LeetCode] 219. Contains Duplicate II ☆(存在重复元素2)
每天一算:Contains Duplicate II 描述 给出1个整形数组nums和1个整数k,是否存在索引i和j,使得nums[i] == nums[j] 且i和j之间的差不超过k Example ...
- [LeetCode] Contains Duplicate II 包含重复值之二
Given an array of integers and an integer k, return true if and only if there are two distinct indic ...
- [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 ...
- 【python】Leetcode每日一题-存在重复元素3
[python]Leetcode每日一题-存在重复元素3 [题目描述] 给你一个整数数组 nums 和两个整数 k 和 t .请你判断是否存在 两个不同下标 i 和 j,使得 abs(nums[i] ...
- [leetcode] Contains Duplicate II
Contains Duplicate II Given an array of integers and an integer k, find out whether there there are ...
- lintcode 中等题:subsets II 带重复元素的子集
题目 带重复元素的子集 给定一个可能具有重复数字的列表,返回其所有可能的子集 样例 如果 S = [1,2,2],一个可能的答案为: [ [2], [1], [1,2,2], [2,2], [1,2] ...
- leetcode第217.题存在重复元素
1.题目描述 给定一个整数数组,判断是否存在重复元素. 如果任何值在数组中出现至少两次,函数返回 true.如果数组中每个元素都不相同,则返回 false. 2.示例 2.1 输入: [1,2,3,1 ...
随机推荐
- MoveManager管理类
MoveManager:移动管理类 struct MoveOpt { int cur_seq; ObjecInfo* obj; }; std::map<ObjID, MoveOpt> m_ ...
- ADO.NET EF实体框架
ADO.NET 实体框架概述 随着.NET Framework 3.5 SP1和Visual Studio 2008 SP1的正式发布.ADO.NET 实体框架正式来到开发人员的面前,它使开发人员可以 ...
- SGU 114
114. Telecasting station time limit per test: 0.25 sec. memory limit per test: 4096 KB Every city in ...
- Android开发--使用真机进行USB调试程序
在android小程序的开发过程中,使用eclipse中的虚拟机进行程序开发速度较慢,用真机开发可以显著提高调试的速度. 这里我用的操作系统是win7专业版,手机型号HM1S: 进行USB调试的主要步 ...
- DB2 中将date类型的转换成timestamp
方法1: TIMESTAMP(Char(date)||'00.00.00') SELECT T2.RECORDNO,T2.DANGERTIME,T2.BIZORIGIN,T3.COMMONNAME A ...
- hdu 3595 GG and MM 博弈论
同时进行,必须操作这就是Every-SG的特点 同样在贾志豪的论文中有提到这种游戏:组合游戏略述——浅谈SG游戏的若干拓展及变形 其中这个游戏特点不仅有必胜和必败,而且有时间长短的博弈,对于自己必胜的 ...
- hdu 4699 Editor 模拟栈
思路:刚开始用STL中的栈,一直RE……,之后改为手动模拟栈操作,在注意点细节就可以了!!! 代码如下: #include<cstdio> #include<cstring> ...
- selenium2 安装、简单使用及浏览器启动问题解决汇总
一.selenium2 安装 1.安装jdk并配置环境变量 jdk需要1.6版本及以上的,这个从网上下载就可以,环境变量的配置前边的随笔整理过就不多说了. 2.安装Firefox,Selenium I ...
- [SharePoint 2013 入门教程 2 ] 创建WEB应用程序,网站集,网站
SharePoint 2013 的 Hello World 由大到小 创建WEB应用程序(老母),网站集(儿子),网站(孙子) 直接确定,其余都默认 填入标题,选好模板.网站集 儿子就有了. 点击页 ...
- Minimum_Window_Substring两种方法求解
题目描述: Given a string S and a string T, find the minimum window in S which will contain all the chara ...