[LeetCode] Contains Duplicate

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     }
posted @ 2015-06-09 11:01  jianchao-li  阅读(178)  评论(0编辑  收藏  举报