Given an array of integers, find if the array contains any duplicates.
Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.
Example 1:
1 |
Input: [1,2,3,1] |
Example 2:
1 |
Input: [1,2,3,4] |
Example 3:
1 |
Input: [1,1,1,3,3,4,3,2,4,2] |
用无序关联式容器unordered_map记录数字出现的次数,key是数字,value代表数字出现的次数。用find(key)或者count(key)可以验证是否有重复数字。
my code in cpp
1 |
bool (vector<int>& nums) { |
Sort
排序过后,比较两个相邻的数字是否相同,如果相同,就说明有重复数字。
my code in cpp
1 |
bool (vector<int>& nums) { |
近期评论