Does this array contain any long-tail word duplicates?
- 内容介绍
- 文章标签
- 相关推荐
本文共计248个文字,预计阅读时间需要1分钟。
给定一个整数数组,判断数组中是否存在重复元素。如果数组中任何值至少出现两次,则函数应返回true,如果数组中每个元素都是唯一的,则返回false。示例1:输入:[1,2,3,1] 输出:true
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:
Input: [1,2,3,1] Output: true
Example 2:
Input: [1,2,3,4] Output: false
Example 3:
Input: [1,1,1,3,3,4,3,2,4,2] Output: true
解法:
先排序,用sort()
然后判断是否相等
//时间<86%
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
sort(nums.begin(),nums.end());//优化的太好了
int s = 0;
for(int i = 1; i < nums.size(); i++){
if(nums[i] - nums[i-1]==0)
return true;
}
return false;
}
};
本文共计248个文字,预计阅读时间需要1分钟。
给定一个整数数组,判断数组中是否存在重复元素。如果数组中任何值至少出现两次,则函数应返回true,如果数组中每个元素都是唯一的,则返回false。示例1:输入:[1,2,3,1] 输出:true
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:
Input: [1,2,3,1] Output: true
Example 2:
Input: [1,2,3,4] Output: false
Example 3:
Input: [1,1,1,3,3,4,3,2,4,2] Output: true
解法:
先排序,用sort()
然后判断是否相等
//时间<86%
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
sort(nums.begin(),nums.end());//优化的太好了
int s = 0;
for(int i = 1; i < nums.size(); i++){
if(nums[i] - nums[i-1]==0)
return true;
}
return false;
}
};

