LeetCode 219:Contains Duplicate II
2016-01-04 11:15
239 查看
Given an array of integers and an integer k,
find out whether there are two distinct indices i and j in
the array such that nums[i] = nums[j] and
the difference between i and jis
at most k.
find out whether there are two distinct indices i and j in
the array such that nums[i] = nums[j] and
the difference between i and jis
at most k.
//给出一个整数数组,判断该数组内是否有两个元素值是相同的,且他们的索引值相差不大于k,是则返回true,否则返回false class Solution { public: bool containsNearbyDuplicate(vector<int>& nums, int k) { map<int, int> buf; int n = nums.size(); for (int i = 0; i < n; i++) { if (buf.find(nums[i]) != buf.end() && i - buf[nums[i]] <= k) { return true; } else { buf[nums[i]] = i; } } return false; } };
相关文章推荐
- 简单使用 SSKeychain 存储密码
- NSLayoutConstraint 简单理解
- hadoop jps出现process information unavailable提示解决办法
- Fzuoj 2216 The Longest Straight 【二分 || 模拟】
- Saiku去掉登录模块
- Facebook人工智能负责人Yann LeCun谈深度学习的局限性
- QDU65 again and again(字典序暴力or字典树)
- 151219DuoTaiDemo
- 趣说游戏AI开发:对状态机的褒扬和批判
- [Android Pro] 网络流量安全测试工具Nogotofail
- VT-x is not available (VERR_VMX_NO_VMX) 的解决方案
- OC对象的retainCount(引用计数器)
- VMfailVaild 错误号
- 【science封面文章】Human-level concept learning through probabilistic program induction
- LeetCode之Climbing Stairs与斐波那契数列的联想
- KMP字符串模式匹配详解http://blog.csdn.net/lin_bei/article/details/1252686
- Develop--Training(六)Getting Started--Working with System Permissions
- POJ 2135 Farm Tour && HDU 2686 Matrix && HDU 3376 Matrix Again 费用流求来回最短路
- 彻底解决INSTALL_FAILED_UPDATE_INCOMPATIBLE的安装错误
- Installation error: INSTALL_FAILED_NO_MATCHING_ABIS