Find the Duplicate Number -- LeetCode
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one.
Note:
- You must not modify the array (assume the array is read only).
- You must use only constant, O(1) extra space.
- Your runtime complexity should be less than
O(n2). - There is only one duplicate number in the array, but it could be repeated more than once.
思路:与这一道题的思路相同 传送门
class Solution {
public:
int findDuplicate(vector<int>& nums) {
int slow = nums[], fast = nums[nums[]];
while (slow != fast)
{
slow = nums[slow];
fast = nums[nums[fast]];
}
fast = ;
while (slow != fast)
{
slow = nums[slow];
fast = nums[fast];
}
return slow;
}
};
Find the Duplicate Number -- LeetCode的更多相关文章
- [LeetCode] Find the Duplicate Number 寻找重复数
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...
- LeetCode 287. Find the Duplicate Number (找到重复的数字)
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...
- 【LeetCode】287. Find the Duplicate Number
Difficulty:medium More:[目录]LeetCode Java实现 Description Given an array nums containing n + 1 integer ...
- LeetCode 287. Find the Duplicate Number (python 判断环,时间复杂度O(n))
LeetCode 287. Find the Duplicate Number 暴力解法 时间 O(nlog(n)),空间O(n),按题目中Note"只用O(1)的空间",照理是过 ...
- Leetcode之二分法专题-287. 寻找重复数(Find the Duplicate Number)
Leetcode之二分法专题-287. 寻找重复数(Find the Duplicate Number) 给定一个包含 n + 1 个整数的数组 nums,其数字都在 1 到 n 之间(包括 1 和 ...
- [LeetCode] 287. Find the Duplicate Number 寻找重复数
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...
- 【LeetCode】287. Find the Duplicate Number 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 保存已经访问过的数字 链表成环 二分查找 日期 题目 ...
- Leetcode Find the Duplicate Number
最容易想到的思路是新开一个长度为n的全零list p[1~n].依次从nums里读出数据,假设读出的是4, 就将p[4]从零改成1.如果发现已经是1了,那么这个4就已经出现过了,所以他就是重复的那个数 ...
- LeetCode——Find the Duplicate Number
Description: Given an array nums containing n + 1 integers where each integer is between 1 and n (in ...
随机推荐
- Kotlin的数据类:节省很多行代码(KAD 10)
作者:Antonio Leiva 时间:Jan 25, 2017 原文链接:https://antonioleiva.com/data-classes-kotlin/ 在前面的文章中,我们已经见到了类 ...
- eclipse里导入maven项目有红叉的解决办法
导入maven的项目上有红叉,说明eclipse里maven的插件该更新了 1.help里选择install new software 2.点击add,输入name:MavenArchiver, lo ...
- Oracle 插入数据时获取系统时间
insert into table_xx (xx,dateTime) values( 'xx',sysdate) 这个sysdate 相当于sql server中的GETDATE()函数 另to_ch ...
- ssh.sh_for_centos
#!/bin/bash sed -i 's/#PermitRootLogin yes/PermitRootLogin yes/g' /etc/ssh/sshd_config sed -i 's/#Us ...
- XGBoost——机器学习--周振洋
XGBoost——机器学习(理论+图解+安装方法+python代码) 目录 一.集成算法思想 二.XGBoost基本思想 三.MacOS安装XGBoost 四.用python实现XGBoost算法 在 ...
- 课时17:函数:Python的乐高积木
目录: 一.创建和调用函数 二.函数的参数 三.函数的返回值 四.课时17课后习题及答案 为了使得程序得代码变得简单,就需要把程序分解成较小得组成部分.有三种方法可以实现:函数.对象.模块. **** ...
- 【Python】- 最牛逼的内建函数max和min
本文主要介绍了max的运行机制,以及如何传入和比较,min函数的运行机制和max一样,只是min取的是最小值 max/min 后直接跟序列会返回此序列的最大/最小值 max(iterable, key ...
- RabbitMQ高可用镜像队列
## RabbitMQ高可用镜像队列 在分布式系统中,通常使用多个术语来标识主要副本和辅助副本.本指南通常使用"主"来引用队列的主要副本,而对于辅助副本则使用"镜像&qu ...
- 【bzoj4719】[Noip2016]天天爱跑步 权值线段树合并
题目描述 给出一棵n个点的树,以及m次操作,每次操作从起点向终点以每秒一条边的速度移动(初始时刻为0),最后对于每个点询问有多少次操作在经过该点的时刻为某值. 输入 第一行有两个整数N和M .其中N代 ...
- Gcd反应堆 (pgcd)
Gcd反应堆 (pgcd) 题目描述 不知什么时候起,TA突然对gcd产生了浓厚的兴趣,于是他为此编写了个程序,输入分别不大于m,n (1<m,n<=10^7)的两个数,就能得出gcd(m ...