当前位置:网站首页>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;
}
边栏推荐
- Talk about what parameters ZABBIX monitors
- Kubernetes create service access pod
- 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
- pytorch训练自己网络后可视化特征图谱的代码
- 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
- The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received
- Vsphere+ and vsan+ are coming! VMware hybrid cloud focus: native, fast migration, mixed load
- 深度学习--数据操作
- Today's sleep quality record 71 points
- twenty million two hundred and twenty thousand seven hundred and one
猜你喜欢

MySQL -- index of MyISAM storage engine

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

Cutefishos system~

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

Ffmpeg learning notes

How to write a performance test plan

Fiori 应用通过 Adaptation Project 的增强方式分享

Slope compensation
![[target tracking] | single target tracking indicator](/img/07/033d5c07b07e3443330840e98940b3.png)
[target tracking] | single target tracking indicator

Understanding of indexes in MySQL
随机推荐
人体姿态估计的热图变成坐标点的两种方案
搜狗微信APP逆向(二)so层
Resttemplate remote call tool class
Awoo's favorite problem (priority queue)
Configure filter
Single step debugging analysis of rxjs observable of operator
Hide the creation and use of users
Appium自动化测试基础 — APPium安装(一)
What class loading mechanisms does the JVM have?
Pytorch's code for visualizing feature maps after training its own network
Pytorch nn.functional.unfold()的简单理解与用法
FFMpeg学习笔记
A few minutes before work, I found out V-model and The difference between sync
General use of qstringlist
Understanding of transactions in MySQL
【扫盲】机器学习图像处理中的深层/浅层、局部/全局特征
Chen Tianqi's machine learning compilation course (free)
nn.Parameter】Pytorch特征融合自适应权重设置(可学习权重使用)
Tourism Management System
[QT widget] encapsulates a simple thread management class