思路:

转换成链表之后使用floyed判环法。转换之后重复的那个数字是唯一一个有多个前驱和一个后继的节点,因此是环的起始节点。

实现:

 class Solution
{
public:
int findDuplicate(vector<int>& nums)
{
int p = nums[], q = nums[];
while (true)
{
p = nums[nums[p]];
q = nums[q];
if (p == q) break;
}
p = nums[];
while (true)
{
if (p == q) break;
p = nums[p];
q = nums[q];
}
return p;
}
};

leetcode287 Find the Duplicate Number的更多相关文章

  1. [Swift]LeetCode287. 寻找重复数 | Find the Duplicate Number

    Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...

  2. [LeetCode] Find the Duplicate Number 寻找重复数

    Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...

  3. 287. Find the Duplicate Number hard

    287. Find the Duplicate Number   hard http://www.cnblogs.com/grandyang/p/4843654.html 51. N-Queens h ...

  4. Leetcode Find the Duplicate Number

    最容易想到的思路是新开一个长度为n的全零list p[1~n].依次从nums里读出数据,假设读出的是4, 就将p[4]从零改成1.如果发现已经是1了,那么这个4就已经出现过了,所以他就是重复的那个数 ...

  5. Find the Duplicate Number

    Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...

  6. LeetCode——Find the Duplicate Number

    Description: Given an array nums containing n + 1 integers where each integer is between 1 and n (in ...

  7. 287. Find the Duplicate Number

    Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...

  8. [LeetCode] 287. Find the Duplicate Number 解题思路

    Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...

  9. Find the Duplicate Number 解答

    Question Given an array nums containing n + 1 integers where each integer is between 1 and n (inclus ...

随机推荐

  1. 是否要从单片机转为嵌入式Linux?

    作者:嵌入式老鸟火哥 授权转载于公众号嵌入式老鸟的职场之道(ID: ict_embedded),有增加内容和修改. 最近很多童鞋投票并咨询如何从单片机转为嵌入式Linux开发.看来读者圈中做单片机,R ...

  2. IIS 部署 SSAS

    转自:http://blog.csdn.net/jinjazz/article/details/4058368 1.首先到分析服务器的SQLServer安装目录中找到如下目录和文件 2.然后为IIS建 ...

  3. 区间sum 和为k的连续区间-前缀和

    区间sum 描述 有一个长度为n的正整数序列a1--an,candy想知道任意区间[L,R]的和,你能告诉他吗? 输入 第一行一个正整数n(0<n<=1e6),第二行为长度为n的正整数序列 ...

  4. C# web 总结

    (1)Cshtml 中 “@” 符号转义 在 cshtml 中需要使用 “@” 符号,如 “@幸福摩天轮版权所有”.那么我们需要使用转义,使用 “@@” 就好!“© ”和 “@” 好像呀. <t ...

  5. E20180514-hm

    invalid  adj. 无效的; 不能成立的; 有病的; 病人用的; readiness n. 准备就绪; 愿意,乐意

  6. Swift 数组,字典,结构体,枚举

    1.数组 let types = ["none","warning","error"]//省略类型的数组声明 var menbers = [ ...

  7. Spring中配置Dbutils

    <!--配置QueryRunner--> <bean id="runner" class="org.apache.commons.dbutils.Que ...

  8. 洛谷P3312 [SDOI2014]数表(莫比乌斯反演+树状数组)

    传送门 不考虑$a$的影响 设$f(i)$为$i$的约数和 $$ans=\sum\limits_{i=1}^n\sum\limits_{j=1}^nf(gcd(i,j))$$ $$=\sum\limi ...

  9. CSS优先级、CSS选择器、编写CSS时的注意事项

    CSS的优先级: 内嵌样式>ID选择器>类选择器>标签选择器 内部样式>内部样式>外部样式 CSS的选择器: 选择器:在 CSS 中,选择器是一种模式,用于选择需要添加样 ...

  10. 1.python真的是万恶之源么?(初识python)

    python真的是万恶之源么? 计算机基础及puthon了解 1.计算机基础知识 cpu : 相当于人类大脑,运算和处理问题 内存 : 临时存储数据,单点就消失,4G,8G,16G,32G 硬盘 : ...