且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

[LeetCode] Contains Duplicate

更新时间:2022-08-19 14:57:59

A classic problem of hash set. The unordered_set of C++ is very suitable for this problem.

The code is as follows and it should be quite self-explanatory.

1     bool containsDuplicate(vector<int>& nums) {
2         unordered_set<int> st;
3         for (int i = 0; i < nums.size(); i++) {
4             if (st.find(nums[i]) != st.end())
5                 return true;
6             st.insert(nums[i]);
7         }
8         return false;
9     }