217. Contains Duplicate
生活随笔
收集整理的這篇文章主要介紹了
217. Contains Duplicate
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
找重復(fù)元素,兩種方法
第一種:排序,有重復(fù)元素,必然出現(xiàn)在相鄰位置
1 int x=[](){ 2 std::ios::sync_with_stdio(false); 3 cin.tie(NULL); 4 return 0; 5 }(); 6 7 class Solution 8 { 9 public: 10 bool containsDuplicate(vector<int>& nums) 11 { 12 int size = nums.size(); 13 if(size <= 1) return false; 14 sort(nums.begin(), nums.end()); 15 for(int i = 0; i < size-1; ++i) 16 { 17 if(nums[i] == nums[i+1]) return true; 18 } 19 return false; 20 } 21 22 };第二種:利用關(guān)聯(lián)容器計數(shù),計數(shù)值大于1,必然有重復(fù)元素。
1 static int wing=[]() 2 { 3 std::ios::sync_with_stdio(false); 4 cin.tie(NULL); 5 return 0; 6 }(); 7 8 class Solution 9 { 10 public: 11 bool containsDuplicate(vector<int>& nums) 12 { 13 unordered_map<int,int> vmap; 14 for(int i : nums) 15 { 16 vmap[i]++; 17 if(vmap[i]>1) 18 return true; 19 } 20 return false; 21 } 22 };?
轉(zhuǎn)載于:https://www.cnblogs.com/zhuangbijingdeboke/p/9077157.html
總結(jié)
以上是生活随笔為你收集整理的217. Contains Duplicate的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 单例模式之懒汉式/饿汉式/结合二者之优的
- 下一篇: spring-quartz