首页 > 其他 > 详细

LeetCode 219:Contains Duplicate II

时间:2016-01-04 11:45:15      阅读:239      评论:0      收藏:0      [点我收藏+]

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.

 //给出一个整数数组,判断该数组内是否有两个元素值是相同的,且他们的索引值相差不大于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;
	 }
 };

技术分享

LeetCode 219:Contains Duplicate II

原文:http://blog.csdn.net/geekmanong/article/details/50454861

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!