Question is to check if array contains duplicate: https://leetcode.com/problems/contains-duplicate/description/
Its the same logic, but leetcode says c++ code took ~69mb and python ~29. Why is this so? Aren't C++ implementations supposed to be more efficient in general? Or maybe the leetcode estimates are not reliable?
C++ solution (
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
unordered_set<int> s;
for(int x : nums){
if(s.find(x)!=s.end())
return true;
s.insert(x);
}
return false;
}
};
Python solution
class Solution(object):
def containsDuplicate(self, nums):
unq = set()
for x in nums:
if x in unq:
return True
unq.add(x)
return False
Expected C++ to outperform Python in space consumed
Aucun commentaire:
Enregistrer un commentaire