[Array]217.Contains Duplicate

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.

思路:只要一个整数数组中某个元素出现两次及以上,返回true,否则返回false。将数组进行排序,只要找到出现两次的元素就可以了。

另外,需要注意的是,找到出现两次的元素的思想是,nums[i]=nums[i+1],这样会忽略数组长度为0或者1的情况,因此应该将这两种情况单独列出来。

 bool containsDuplicate(vector<int>& nums) {
if(nums.empty())
return false ;
int n = nums.size();
if(n == 1)//或者将这里写成if(n == 0 || n == 1)
return false;
sort(nums.begin(), nums.end());
for(int i = 0; i < n; i++){
if(nums[i] == num[i+1])
return true;
}
else if (i == n-1)
return false;
}
时间: 2024-10-20 08:08:25

[Array]217.Contains Duplicate的相关文章

217. Contains Duplicate(C++)

217. Contains Duplicate 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. 题目大意: 给定一个整数数组,

leetCode 217. Contains Duplicate 数组

217. Contains Duplicate 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. 题目大意: 在数组中找到任意字

leetcode 217. Contains Duplicate 287. Find the Duplicate Number

217. Contains Duplicate 后面3个题都是限制在1-n的 class Solution { public: bool containsDuplicate(vector<int>& nums) { int length = nums.size(); if(length <= 0) return false; sort(nums.begin(),nums.end()); for(int i = 1;i < length;i++){ if(nums[i] ==

LeetCode # Array # Easy # 217. Contains Duplicate

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. 题目:给定一个数组,判断其中有无重复元素,返回true或false. 思路:首

217 Contains Duplicate (Array)

1 class Solution { 2 public boolean containsDuplicate(int[] nums) { 3 HashMap<Integer, Integer> m = new HashMap<Integer, Integer>(); 4 for(int i = 0; i < nums.length; i++) { 5 if (m.containsKey(nums[i])) { 6 return true; 7 }else { 8 m.put(n

leetcode 217 Contains Duplicate 数组中是否有重复的数字

 Contains Duplicate Total Accepted: 26477 Total Submissions: 73478 My Submissions 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 shoul

leetcode 217 Contains Duplicate 数组中是否有反复的数字

?? Contains Duplicate Total Accepted: 26477 Total Submissions: 73478 My Submissions 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 shoul

【LeetCode】217. Contains Duplicate (2 solutions)

Contains Duplicate 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. 解法一: 排序,然后比较相邻元素是否相同

217. Contains Duplicate

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. 给定整数数组,查找数组是否包含任何重复项. 如果数组中的任何值至少出现两次,则