leetcode_217题——Contains Duplicate(哈希表)

Contains Duplicate

  Total Accepted: 3498 Total Submissions: 9359My Submissions

 

Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.

 

Hide Tags
  Array Hash Table
Have you met this question in a real interview? 
Yes
 
No
 

Discuss

       这道题很简单,就是采用哈希表就没啦

#include<iostream>

#include<vector>

#include<set>

using namespace std;

bool containsDuplicate(vector<int>& nums) {

	if(nums.empty()||nums.size()==1)

		return false;

	set<int> temp;

	temp.insert(nums[0]);

	for(int i=1;i<nums.size();i++)

	{

		if(temp.count(nums[i])==1)

			return true;

		else

			temp.insert(nums[i]);

	}

	return false;

}

int main()

{



}

  

 

你可能感兴趣的:(LeetCode)