leetcode解题报告(18):Contains Duplicate
2024-08-27 04:02:49
描述
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();
}
};
最新文章
- 使用git管理源代码
- 文件大boss
- 什么是 IntentService
- linux之稀疏文件
- Android中使用proguardgui混淆jar包
- ISBN-10和ISBN-13有什么区别?
- U7Linux文件与目录管理
- Angularjs 基于karma和jasmine的单元测试
- flume-sink报错 java.lang.IllegalStateException: close() called when transaction is OPEN - you must either commit or rollback first
- mysql之数据操作
- linux下的抓包工具tcpdump
- AndroidStudio 开发JNI
- socket 总结
- 对 data属性的使用之一
- git的安装以及生成ssh key
- Linux下使用systemctl命令
- js将对象数组按照自定义规则排序
- 安装odbc驱动
- 机器学习--boosting家族之Adaboost算法
- 君学,佳一tvodp文件破解