当前位置:网站首页>LeetCode 217. 存在重复元素
LeetCode 217. 存在重复元素
2022-07-26 13:07:00 【PUdd】
方法一:排序
先排序,再找前后是否有一样的元素
class Solution {
public:
bool containsDuplicate(vector<int>& nums)
{
sort(nums.begin(), nums.end());
for(int i=1;i<nums.size();i++)
{
if(nums.at(i-1)==nums.at(i))
return 1;
}
return 0;
}
};
方法二:哈希表
用的是set的不重复特性,也即无论有多少重复的set都会给删成排好序的不重复序列。
全放到set里去,个数少了,那就是有重复的。
(时间、空间复杂度均很高,可能跟set的红黑树(?)有关系,换成unorder_set按官方题解做会好很多,没有深究)
class Solution {
public:
bool containsDuplicate(vector<int>& nums)
{
set<int> set1;
for(int i=0;i<nums.size();i++)
{
set1.insert(nums.at(i));
}
if(set1.size()!=nums.size())return 1;
else return 0;
}
};
边栏推荐
- 虚拟偶像代言产品出问题谁负责?且听律师分析
- MySQL data directory (2) -- table data structure (XXV)
- Code error reporting and problem solving experience II: test error reporting in yolov5
- Flutter prevents scientific counting and removes mantissa invalid 0
- From January to June, China's ADAS suppliers accounted for 9%, and another parts giant comprehensively laid out the new smart drive track
- AI-理论-知识图谱1-基础
- Version of NDK matched the requested version 21.0.6113669. versions available locally: 2
- A college archives management system based on asp.net
- 1-6月中国ADAS供应商占比9% 又一家零部件巨头全面布局智驾新赛道
- 12-GuliMall 后台管理中商品系统的品牌管理
猜你喜欢

Ue5 official case Lyra full feature explanation 7. resource management
![[5g] what are Cu and Du in 5g?](/img/5b/3453ade88ded4593edfcba9beb8ada.jpg)
[5g] what are Cu and Du in 5g?

1312_ Apply 7z command for compression and decompression

一笔画问题(中国邮递员问题)

1-6月中国ADAS供应商占比9% 又一家零部件巨头全面布局智驾新赛道

被罚“带薪休假”一个月后,谷歌解雇了“爱”上 AI 的他
![[typescript] typescript common types (Part 1)](/img/80/5c8c51b92d3a9d76f38beba7be0aa6.png)
[typescript] typescript common types (Part 1)

A college archives management system based on asp.net

Today in history: IBM obtained the first patent; Verizon acquires Yahoo; Amazon releases fire phone
![[typescript] typescript common types (Part 2)](/img/6b/2ac07f16af044bdfb719753ae241cc.png)
[typescript] typescript common types (Part 2)
随机推荐
食品安全 | 网购的自制食品就是健康食品?莫要陷入这些误区
0 basic programming resources (collect first ~ read slowly ~)
Kubernetes----Kubernetes常用插件简介
Kubernetes----安装部署NFS服务器
基于Bézier曲线的三维造型与渲染
概要设计说明书
Incorrect use of parentdatawidget when the exception was thrown, this was the stack:
key&key_ Len & ref & filtered (4) - MySQL execution plan (50)
华为年内二度招聘“天才少年”;540万Twitter账号信息泄露,卖价3万美元;谷歌解雇了相信AI有意识的工程师|极客头条...
B+树挑选索引(1)---mysql从入门到精通(二十二)
Sword finger offer (21): push in and pop-up sequence of stack
Streamnational team culture: a "transparent" company
Bitwise and how to write SQL
基于C#实现的学生考试系统
B+树索引使用(8)排序使用及其注意事项(二十)
高通再次「押宝」中科创达,挑战智能驾驶软硬件全栈方案
Display inline+calc realizes left, middle and right layout, and the middle is adaptive
MySQL data directory (3) -- table data structure MyISAM (XXVI)
Remote IP debugger (Practical dry goods)
The strongest tool class of entity mapping: mapstruct Zhenxiang