Contains Duplicate
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.
Hash Table 的key储存已出现的值,如果对某个值出现在Hash Table中,则有重复。
1 class Solution { 2 public: 3 bool containsDuplicate(vector<int>& nums) { 4 if(nums.size()==0) return false; 5 unordered_map<int,int> showed; 6 for(int i=0;i<nums.size();i++) 7 { 8 if(showed.find(nums[i])!=showed.end()) 9 { 10 return true; 11 } 12 else 13 { 14 showed[nums[i]]=1; 15 } 16 } 17 return false; 18 } 19 };