当前位置:网站首页>leetcode - 287. Find duplicates
leetcode - 287. Find duplicates
2022-07-01 22:54:00 【zmm_ mohua】
leetcode - 287. Look for repetitions
subject

Code
#include <iostream>
#include <vector>
#include <set>
using namespace std;
// With the help of set aggregate
int findDuplicate1(vector<int>& nums) {
int n = nums.size();
set<int> visited;
int num;
for(int i = 0; i < n; i++){
if(!visited.count(nums[i])){
visited.insert(nums[i]);
}else{
num = nums[i];
}
}
return num;
}
// Fast and slow pointer circle judgment
int findDuplicate(vector<int>& nums) {
int slow = 0, fast = 0;
do{
slow = nums[slow];
fast = nums[nums[fast]];
}while(slow != fast);
slow = 0;
while(slow != fast){
slow = nums[slow];
fast = nums[fast];
}
return slow;
}
int main(){
int n, res;
cin>>n;
vector<int> nums(n);
for(int i = 0; i < n; i++){
cin>>nums[i];
}
res = findDuplicate(nums);
cout<<res;
return 0;
}
边栏推荐
- 小红书Scheme跳转到指定页面
- Appium automated testing foundation - Supplement: introduction to desired capabilities parameters
- Detailed explanation of common configurations in redis configuration file [easy to understand]
- MySQL -- index of MyISAM storage engine
- Measurement of reference loop gain and phase margin
- [QT widget] encapsulates a simple thread management class
- Intelligent computing architecture design of Internet
- 好友新书发布,祝贺(送福利)
- Configure filter
- pytorch训练自己网络后可视化特征图谱的代码
猜你喜欢

Using emqx cloud to realize one machine one secret verification of IOT devices

447 Bili Bili noodles warp 1

Rust language - Introduction to Xiaobai 05

Sogou wechat app reverse (II) so layer

14年本科毕业,3个月转行软件测试月薪13.5k,32的岁我终于找对了方向

Single step debugging analysis of rxjs observable of operator

Yolov5.5 call local camera

Turn -- bring it and use it: share a gadget for checking memory leaks

El input text field word limit, beyond which the display turns red and input is prohibited

SAP UI5 应用开发教程之一百零四 - SAP UI5 表格控件的支持复选(Multi-Select)以及如何用代码一次选中多个表格行项目
随机推荐
The median salary of TSMC's global employees is about 460000, and the CEO is about 8.99 million; Apple raised the price of iPhone in Japan; VIM 9.0 release | geek headlines
104. SAP UI5 表格控件的支持复选(Multi-Select)以及如何用代码一次选中多个表格行项目
Talk about what parameters ZABBIX monitors
[jetcache] how to use jetcache
MySQL中对于索引的理解
3DE resources have nothing or nothing wrong
【QT小作】封装一个简单的线程管理类
Origin2018 installation tutorial "recommended collection"
The principle, testing and Countermeasures of malicious software reverse closing EDR
Cloud Vulnerability Global Database
LC501. Mode in binary search tree
转--原来gdb的底层调试原理这么简单
Turn -- go deep into Lua scripting language, so that you can thoroughly understand the debugging principle
C#/VB. Net to add text / image watermarks to PDF documents
Awoo's favorite problem (priority queue)
Rust language - Introduction to Xiaobai 05
cvpr2022 human pose estiamtion
14年本科毕业,3个月转行软件测试月薪13.5k,32的岁我终于找对了方向
Réimpression de l'article csdn
Fiori 应用通过 Adaptation Project 的增强方式分享