LeetCode 217 Contains Duplicate (Contains Duplicate numbers) (Vector, hash)
Translation
Specify an array of integer numbers to check whether the array contains any duplicate content. If any value appears at least twice, true is returned. If each value is different, false is returned ).
Original
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.
Analysis
This question is relatively simple and I will not elaborate on it much.
My first response is still original (I try to describe my thinking process in each problem solving process), using std: find () the function searches for repeated values in the vector.
bool containsDuplicate(vector
& nums) { vector
::iterator temp; int num_to_find; for (vector
::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;}
Then I put it on LeetCode and tried it. The result is timeout. Well, after all, there are tens of thousands of data records in the test case. Not in a hurry. What if I use the sort function to sort the order first?
bool containsDuplicate(vector
& nums) {sort(nums.begin(), nums.end()); vector
::iterator temp; int num_to_find; for (vector
::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;}
This is useless. Oh, right. Even if the code above is sorted, all the subsequent data is scanned and judged.
However, since the order is arranged, it is not good to use the next value to compare it with the current value?
Current = next? Repeat it! Current <next description? I sorted them out. Isn't that proper? You don't have to judge this. Current> next? It all said that the order has been sorted. Is this possible!
So, look at the code ...... (I changed the index to it, just to make the row look shorter. The meaning of the two variables is the same)
bool containsDuplicate(vector
& nums) { if (nums.size() <= 1) return false; sort(nums.begin(), nums.end()); vector
::iterator temp; int num_to_find; for (vector
::iterator it = nums.begin(), temp = it + 1; it < nums.end(); ++it,++temp) { if (*temp == *it) return true; } return false;}
But here we see two temp:
temp = it + 1;++ temp;
It seems quite troublesome, but it should be clear:
bool containsDuplicate(vector
& nums) { if (nums.size() <= 1) return false; sort(nums.begin(), nums.end()); vector
::iterator temp; int num_to_find; for (vector
::iterator it = nums.begin(); it < nums.end(); ++it) { temp = it + 1; if (*temp == *it) return true; } return false;}
Code
class Solution {public: bool containsDuplicate(vector
& nums) { if (nums.size() <= 1) return false; sort(nums.begin(), nums.end()); vector
::iterator temp; int num_to_find; for (vector
::iterator it = nums.begin(); it < nums.end(); ++it) { temp = it + 1; if (*temp == *it) return true; } return false; }};
Advanced
My code is 44 ms. Let's take a look at what 36 ms is written by the great god. Come on!
class Solution {public: bool containsDuplicate(vector
& nums) { if(nums.size() == 0) return false; int min = nums[0], max = nums[0]; for(auto n : nums){ if(n > max) max = n; if(n < min) min = n; } int arr[max - min + 1] = {0}; for(auto n : nums){ ++arr[n - min]; } for(int i = 0; i != (max - min + 1); ++i) if(arr[i] > 1) return true; return false; }};
Interestingly, after sorting by using the sort function, it increases by 4 ms.
class Solution {public: bool containsDuplicate(vector
& nums) { if (nums.size() == 0) return false; sort(nums.begin(), nums.end()); int min = nums[0], max = nums[nums.size() - 1]; int arr[max - min + 1] = { 0 }; for (auto n : nums) { ++arr[n - min]; } for (int i = 0; i != (max - min + 1); ++i) if (arr[i] > 1) return true; return false; }};
Hash
class Solution {public: bool containsDuplicate(vector
& nums) { int count=1000; vector
hash[count]; for(int i=0;i
=0?nums[i]:-nums[i])%count; for(int j=0;j