[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.

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     }
目录
相关文章
|
Go
leetcode-每日一题1408. 数组中的字符串匹配(暴力枚举)和Golang里关于Index方法和Contains方法区别
题目要求我们找到字符串数组中存在字符串是其他单词的子字符串,看到题目给我们的n的范围是[1,100],所以我们可以通过暴力枚举用两个for循环一层指子串一层指找存在这个子串的单词,找到则找下个一个子串
114 0
leetcode-每日一题1408. 数组中的字符串匹配(暴力枚举)和Golang里关于Index方法和Contains方法区别
|
算法 Python
LeetCode 217. 存在重复元素 Contains Duplicate
LeetCode 217. 存在重复元素 Contains Duplicate
LeetCode 219: 存在重复元素 II Contains Duplicate II
题目: ​ 给定一个整数数组和一个整数 k,判断数组中是否存在两个不同的索引 i 和 j,使得 nums [i] = nums [j],并且 i 和 j 的差的绝对值最大为 k。 ​ Given an array of integers and an integer k, find o...
582 0
LeetCode 217:存在重复元素 Contains Duplicate
题目: 给定一个整数数组,判断是否存在重复元素。 Given an array of integers, find if the array contains any duplicates. 如果任何值在数组中出现至少两次,函数返回 true。
9544 0
|
存储 索引
LeetCode 219 Contains Duplicate II(包含重复数字2)(*)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/50593169 翻译 给定一个整型数组和一个整型数K, 找出是否存在两个不同的索引i和j,使得nums[i] = nums[j], 并且它们之间的距离最大为K。
990 0
|
测试技术
LeetCode 217 Contains Duplicate(包含重复数字)(Vector、hash)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/50504102 翻译 给定一个整型数字数组,找出这个数组是否包含任何重复内容。
1041 0
|
28天前
|
机器学习/深度学习 算法
力扣刷题日常(一)
力扣刷题日常(一)
20 2
|
1月前
|
存储 索引
《LeetCode》—— LeetCode刷题日记
《LeetCode》—— LeetCode刷题日记

热门文章

最新文章