translation
给定一个整型数字数组,找出这个数组是否包含任何重复内容。如果任何值出现了至少两次,那么返回真(true),如果每个值都是互不相同的,那么返回假(false)。
Original
ofifthecontainsreturntrueifatintheanditreturnfalseifeveryis distinct.
Analysis
This time the test instructions is relatively simple, not much elaborated.
My first reaction is still relatively primitive (I try to describe my thinking process in every problem solving process), using the Std::find () function to find out if there are duplicate values in the vector.
BOOLContainsduplicate ( vector<int>& Nums) { vector<int>:: Iterator temp;intNum_to_find; for( vector<int>:: Iterator index = Nums.begin (); Index < Nums.end (); ++index) {num_to_find = *index; temp =STD:: Find (Index +1, Nums.end (), num_to_find);if(Temp! = Nums.end ())return true; }return false;}
And then put it on the Leetcode to try, the result is time-out, OK, after all, the test case is how many levels of data. No hurry, if you use the Sort function to order first?
BOOLContainsduplicate ( vector<int>& Nums) {sort (Nums.begin (), Nums.end ()); vector<int>:: Iterator temp;intNum_to_find; for( vector<int>:: Iterator index = Nums.begin (); Index < Nums.end (); ++index) {num_to_find = *index; temp =STD:: Find (Index +1, Nums.end (), num_to_find);if(Temp! = Nums.end ())return true; }return false;}
It doesn't work, oh yes, even if the above code is sorted, it will be scanned for all subsequent data.
But, since the line is ordered, directly with the next value and the current value is not better?
next nextnext 说明什么?都说了已经排过序了,这可能吗!
Well, look at the code ... (I changed index to it, just to make that line appear shorter, the meaning of the two variables is the same)
BOOLContainsduplicate ( vector<int>& Nums) {if(Nums.size () <=1)return false; Sort (Nums.begin (), Nums.end ()); vector<int>:: Iterator temp;intNum_to_find; for( vector<int>:: Iterator it = nums.begin (), temp = it +1; It < Nums.end (); ++it,++temp) {if(*temp = = *it)return true; }return false;}
But in fact, there are two times temp:
temp = it + 1;++ temp;
It seems to be a bit more troublesome, or to change it clearly:
BOOLContainsduplicate ( vector<int>& Nums) {if(Nums.size () <=1)return false; Sort (Nums.begin (), Nums.end ()); vector<int>:: Iterator temp;intNum_to_find; for( vector<int>:: Iterator it = Nums.begin (); It < Nums.end (); ++it) {temp = it +1;if(*temp = = *it)return true; }return false;}
Code
classSolution { Public:BOOLContainsduplicate ( vector<int>& Nums) {if(Nums.size () <=1)return false; Sort (Nums.begin (), Nums.end ()); vector<int>:: Iterator temp;intNum_to_find; for( vector<int>:: Iterator it = Nums.begin (); It < Nums.end (); ++it) {temp = it +1;if(*temp = = *it)return true; }return false; }};
Advanced
My code is 44ms, to see what the great God wrote 36ms is how it, refueling!
classSolution { Public:BOOLContainsduplicate ( vector<int>& Nums) {if(nums.size () = =0)return false;intMin = nums[0], max = nums[0]; for(AutoN:nums) {if(n > Max) max = n;if(N < min) min = n; }intArr[max-min +1] = {0}; for(AutoN:nums) {++arr[n-min]; } for(inti =0; I! = (Max-min +1); ++i)if(Arr[i] >1)return true;return false; }};
Interestingly, I added 4ms after sorting with the sort function.
classSolution { Public:BOOLContainsduplicate ( vector<int>& Nums) {if(nums.size () = =0)return false; Sort (Nums.begin (), Nums.end ());intMin = nums[0], max = Nums[nums.size ()-1];intArr[max-min +1] = {0}; for(AutoN:nums) {++arr[n-min]; } for(inti =0; I! = (Max-min +1); ++i)if(Arr[i] >1)return true;return false; }};
Hash
classSolution { Public:BOOLContainsduplicate ( vector<int>& Nums) {intCount= +; vector<int>Hash[count]; for(intI=0; I<nums.size (); i++) {intIndex= (nums[i]>=0? Nums[i]:-nums[i])%count; for(intj=0; Jif(Nums[i]==hash[index][j])return true; } hash[index].push_back (Nums[i]); }return false; }};
Leetcode 217 Contains Duplicate (contains duplicate numbers) (Vector, hash)