leetcode Contains Duplicate II 哈希表

原创 2015年07月06日 17:27:09

Given an array of integers and an integer k, find out whether there there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between iand j is at most k.

Hide Tags
 Array Hash Table


110道了,O(∩_∩)O,题目意思是给一个数组,判断数组中是否有序号差小于k且相等的两个数。

一开始用两个for循环,去查找,结果超时。看提示才知道用哈希表。

class Solution {
public:
	bool containsNearbyDuplicate(vector<int>& nums, int k) {
		if (!nums.size()) return false;
		map<int, int> HashMap;
		for (int i = 0; i < nums.size(); i++)
		{
			if (HashMap.find(nums[i]) != HashMap.end() && i - HashMap[nums[i]]<=k)
			{
				return true;
			}
			HashMap[nums[i]] = i;
		}
		return false;
	}
};


leetcode Contains Duplicate系列问题,哈希表,红黑树,二叉查找树

首先第一题leetcode 217 contains duplicate是要在一组正整数里面找出重复的那一个,可以构造一个哈希表来查找,也可以构造二叉查找树来查找,哈希表查找的时间效率比二叉查找树好,...

Leetcode 219. Contains Duplicate 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

1.题目Given an array of integers and an integer k, find out whether there are two distinct indices i a...

[LeetCode] Contains Duplicate(II,III)

Contains DuplicateGiven an array of integers, find if the array contains any duplicates. Your functi...

leetcode--Contains Duplicate II

Contains Duplicate II

【leetcode】Array——Contains Duplicate II(219)

题目:Given an array of integers and an integer k, find out whether there are two distinct indices i ...

【leetcode】219. Contains Duplicate II

一、题目描述 Given an array of integers and an integer k, find out whether there are two distinct indic...

LeetCode | Contains Duplicate II

Given an array of integers and an integer k, find out whether there there are two distinct indices ...

LeetCode笔记:219. Contains Duplicate II

判断一个数组中一定范围内是否有相同的数字

【Leetcode Algorithm】Contains Duplicate II

Given an array of integers and an integer k, find out whether there there are two distinct indices...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode Contains Duplicate II 哈希表
举报原因:
原因补充:

(最多只允许输入30个字)