加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

LeetCode 217. Contains Duplicate C++

发布时间:2020-12-16 09:14:17 所属栏目:百科 来源:网络整理
导读: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]Out

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,4]
Output: false

Example 3:

Input: [1,1,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; } };

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读