思路:

转换成链表之后使用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. kali本機安裝openvas的血淚史復盤

    安裝openvas的血淚史 因爲學習的需要,需要裝openvas,但是在虛擬機裏面,無論怎麼更新跟新源,總是會有問題,一氣之下,便不用虛擬機了,將自己的物理機刷成了kali機,從此便進了一個大坑. 安 ...

  2. Linux-Bond-Configure

    Centos 6.6 1. modify /etc/modprobe.d/bond.conf alias bond0 bonding 2. config eth0 & eth1 cat /et ...

  3. JavaScript-Tool-富文本:Simditor

    ylbtech-JavaScript-Tool-富文本:Simditor 1.返回顶部 1. 2. 2.返回顶部 1. Simditor 是团队协作工具 Tower 使用的富文本编辑器. 相比传统的编 ...

  4. android开发中怎么通过Log函数输出当前行号和当前函数名

    public class Debug { public static int line(Exception e) { StackTraceElement[] trace = e.getStackTra ...

  5. vue render函数使用jsx语法 可以使用v-model语法 vuex实现数据持久化

    render函数使用jsx语法: 安装插件  transform-vue-jsx 可以使用v-model语法安装插件 jsx-v-model .babelrc文件配置: vuex实现数据持久化 安装插 ...

  6. eclipse neon 离线安装插件

    我的eclipse版本是4.6.3,以安装svn插件为例. 网上查到很多资料都是说找到eclipse目录下"plugins"和"features"文件夹,将下载 ...

  7. django继承修改 User表导致的问题 fields.E304(permissions/group都会有这样的错误)

    问题: django继承修改 User表时,进行migrations操作时会导致的问题 fields.E304(permissions/group都会有这样的错误)如图: 根源: django文档中有 ...

  8. AWS AutoScaling的一个ScaleDown策略问题以及解决方法

    此文已由作者袁欢授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 1. AWS AutoScaling简介 AutoScaling是AWS的一个重要服务,用来弹性的自动创建(S ...

  9. 洛谷P2824 [HEOI2016/TJOI2016]排序(线段树)

    传送门 这题的思路好清奇 因为只有一次查询,我们考虑二分这个值为多少 将原序列转化为一个$01$序列,如果原序列上的值大于$mid$则为$1$否则为$0$ 那么排序就可以用线段树优化,设该区间内$1$ ...

  10. [Xcode 实际操作]八、网络与多线程-(4)使用UIApplication对象发送短信

    目录:[Swift]Xcode实际操作 本文将演示如何使用应用程序单例对象,发送短信的功能. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] 注:需要使用真机进行测 ...