当前位置:网站首页>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;
}
边栏推荐
- [jetcache] how to use jetcache
- General use of qstringlist
- 陈天奇的机器学习编译课(免费)
- 转--深入LUA脚本语言,让你彻底明白调试原理
- Delete AWS bound credit card account
- SAP ui5 application development tutorial 104 - multi select support for SAP ui5 table controls and how to use code to select multiple table row items at a time
- 恶意软件反向关闭EDR的原理、测试和反制思考
- 搜狗微信APP逆向(二)so层
- Today's sleep quality record 71 points
- mixconv代码
猜你喜欢

多图预警~ 华为 ECS 与 阿里云 ECS 对比实战

Ffmpeg learning notes

How to write a performance test plan

Digital currency: far-reaching innovation

Slope compensation

分享一个一年经历两次裁员的程序员的一些感触

Deadlock handling strategies - prevent deadlock, avoid deadlock, detect and remove deadlock

"Trust machine" empowers development

MySQL -- index of MyISAM storage engine

internal field separator
随机推荐
【日常训练】326. 3 的幂
Using securecrtportable to remotely connect virtual machines
SAP GUI 里的收藏夹事务码管理工具
I graduated from college in 14 years and changed to software testing in 3 months. My monthly salary was 13.5k. At the age of 32, I finally found the right direction
Understanding of inverted residuals
The fixed assets management subsystem reports are divided into what categories and which accounts are included
Chen Tianqi's machine learning compilation course (free)
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received
Detailed explanation of common configurations in redis configuration file [easy to understand]
下班前几分钟,我弄清了v-model与.sync的区别
Origin2018安装教程「建议收藏」
Wechat open platform scanning code login [easy to understand]
[daily training] 326 Power of 3
Rust language - Introduction to Xiaobai 05
Vsphere+ and vsan+ are coming! VMware hybrid cloud focus: native, fast migration, mixed load
转载csdn文章操作
What class loading mechanisms does the JVM have?
Selection of all-optical technology in the park - Part 2
The principle, testing and Countermeasures of malicious software reverse closing EDR
How to write a performance test plan