当前位置:网站首页>leetcode - 287. 寻找重复数
leetcode - 287. 寻找重复数
2022-07-01 21:45:00 【zmm_mohua】
leetcode - 287. 寻找重复数
题目
代码
#include <iostream>
#include <vector>
#include <set>
using namespace std;
// 借助set集合
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;
}
// 快慢指针判圈
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;
}
边栏推荐
- [STM32] stm32cubemx tutorial II - basic use (new projects light up LED lights)
- 二叉树的基本操作
- EasyExcel 复杂数据导出
- 【MySQL】explain的基本使用以及各列的作用
- PyTorch磨刀篇|argmax和argmin函数
- pytest合集(2)— pytest運行方式
- linux下清理系统缓存并释放内存
- Manually implement function isinstanceof (child, parent)
- News classification based on LSTM model
- 《QTreeView+QAbstractItemModel自定义模型》:系列教程之三[通俗易懂]
猜你喜欢
基于三维GIS的不动产管理应用
Business visualization - make your flowchart'run'up
上半年暂停考试要补考?包含监理工程师、建筑师等十项考试
ICML2022 | 基于元语义正则化的介入性对比学习
[NOIP2013]积木大赛 [NOIP2018]道路铺设 贪心/差分
【MySQL】explain的基本使用以及各列的作用
linux下清理系统缓存并释放内存
List announced | outstanding intellectual property service team in China in 2021
Little p weekly Vol.11
Sonic cloud real machine learning summary 6 - 1.4.1 server and agent deployment
随机推荐
Interview question: what is the difference between MySQL's Union all and union, and how many join methods MySQL has (Alibaba interview question) [easy to understand]
Relationship and difference between enterprise architecture and project management
Talking from mlperf: how to lead the next wave of AI accelerator
Using closures to switch toggle by clicking a button
MySQL之MHA高可用配置及故障切换
GaussDB(DWS)主动预防排查
"The silk road is in its youth and looks at Fujian" is in the hot collection of works in the Fujian foreign youth short video competition
An operation tool used by we media professionals who earn 1w+ a month
微信小程序,连续播放多段视频。合成一个视频的样子,自定义视频进度条
旁路由设置的正确方式
Burpsuite simple packet capturing tutorial [easy to understand]
Separate the letters and numbers in the string so that the letters come first and the array comes last
Sonic cloud real machine learning summary 6 - 1.4.1 server and agent deployment
月入1W+的自媒体达人都会用到的运营工具
Use of vscode
业务可视化-让你的流程图'Run'起来
TOPS,处理器运算能力单位、每秒钟可进行一万亿次
[NOIP2013]积木大赛 [NOIP2018]道路铺设 贪心/差分
《QTreeView+QAbstractItemModel自定义模型》:系列教程之三[通俗易懂]
Aidl basic use