本文共 452 字,大约阅读时间需要 1 分钟。
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 & nums) {2 unordered_set 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 }
转载地址:http://kdsnm.baihongyu.com/