当前位置:网站首页>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;
}
边栏推荐
- 思科--WAN 的概念考试外部工具
- Preparation of functional test report
- Mysql database detailed learning tutorial
- 多图预警~ 华为 ECS 与 阿里云 ECS 对比实战
- MySQL中对于事务的理解
- Pytorch nn. functional. Simple understanding and usage of unfold()
- 隐藏用户的创建和使用
- "Trust machine" empowers development
- Understanding of indexes in MySQL
- Selection of all-optical technology in the park - Part 2
猜你喜欢
Slope compensation
Use three JS realize the 'ice cream' earth, and let the earth cool for a summer
Flynk SQL client uses comparison and is familiar with official documents
使用 EMQX Cloud 实现物联网设备一机一密验证
Compensation des créneaux horaires
Multi picture alert ~ comparison of Huawei ECs and Alibaba cloud ECS
元宇宙可能成为互联网发展的新方向
El input text field word limit, beyond which the display turns red and input is prohibited
Understanding of transactions in MySQL
SAP intelligent robot process automation (IRPA) solution sharing
随机推荐
聊一聊Zabbix都监控哪些参数
Explain kubernetes network model in detail
Efficiency improvement - encourage personalized container development environment
Wechat open platform scanning code login [easy to understand]
Mysql database detailed learning tutorial
104. SAP ui5 table control supports multi select and how to select multiple table row items at a time with code
Fiori applications are shared through the enhancement of adaptation project
Measurement of reference loop gain and phase margin
Pytorch nn.functional.unfold()的简单理解与用法
14年本科毕业,3个月转行软件测试月薪13.5k,32的岁我终于找对了方向
Turn -- go deep into Lua scripting language, so that you can thoroughly understand the debugging principle
陈天奇的机器学习编译课(免费)
SAP 智能机器人流程自动化(iRPA)解决方案分享
Understanding of indexes in MySQL
台积电全球员工薪酬中位数约46万,CEO约899万;苹果上调日本的 iPhone 售价 ;Vim 9.0 发布|极客头条
【日常训练】66. 加一
Metauniverse may become a new direction of Internet development
General use of qstringlist
Two schemes of transforming the heat map of human posture estimation into coordinate points
友善串口助手使用教程_友善串口调试助手怎么进行配置-友善串口调试助手使用教程…