描述

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.

分析

先排个序,然后判断当前元素和下一元素是否相等就行了。

代码如下:

class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
if(nums.size() == 0)return false;
sort(nums.begin(),nums.end());
for(int i = 0; i != nums.size() - 1; ++i){
if(nums[i] == nums[i + 1])return true;
}
return false;
}
};

讨论区发现了一行代码写出来的:

class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
return nums.size() > set<int>(nums.begin(),nums.end()).size();
}
};

最新文章

  1. 使用git管理源代码
  2. 文件大boss
  3. 什么是 IntentService
  4. linux之稀疏文件
  5. Android中使用proguardgui混淆jar包
  6. ISBN-10和ISBN-13有什么区别?
  7. U7Linux文件与目录管理
  8. Angularjs 基于karma和jasmine的单元测试
  9. flume-sink报错 java.lang.IllegalStateException: close() called when transaction is OPEN - you must either commit or rollback first
  10. mysql之数据操作
  11. linux下的抓包工具tcpdump
  12. AndroidStudio 开发JNI
  13. socket 总结
  14. 对 data属性的使用之一
  15. git的安装以及生成ssh key
  16. Linux下使用systemctl命令
  17. js将对象数组按照自定义规则排序
  18. 安装odbc驱动
  19. 机器学习--boosting家族之Adaboost算法
  20. 君学,佳一tvodp文件破解

热门文章

  1. C语言下进制的使用
  2. Qt4 和 Qt5 模块的分类
  3. 测试库异常down分析(abnormal instance termination)
  4. sqlserver case when 的使用方法
  5. java开发中常用语(词汇)含义
  6. jQuery效果之滑动
  7. 一行命令开启VNC 和windows之间复制粘贴功能
  8. Python3 实例
  9. Linux sudo(CVE-2019-14287)漏洞复现过程
  10. Jenkins管理插件