某视面试官问了一道这样的题,1到N(N为正整数)共N个正整数,其中有一个数重复一次覆盖了另外一个数,比如:9,3,7,5,1,8,2,4,5,那么其中5重复一次,相当于覆盖了6,那么,请找出这个重复的数。

当时提供了HASH法,但人家不满意,直接pass了。

如果能想到循环链表式的数据结构,那么就很容易有思路了。// 9,3,7,5,1,8,2,4,5 error

 #include <iostream>
 #include <stdlib.h>
 #include <stdio.h>
 #include <string>
 #include <string.h>
 #include <algorithm>//reverse
 #include <vector>
 using namespace std;
 #define debug(x) cout << #x << " at line " << __LINE__ << " is: " << x << endl

 void f(int *s,int n){
     , f=; // should n-1
     do{
         w = s[w]-;
         f = s[s[f]-]-;
     }while(w != f);
     printf();
 }

 int main() {
     ;
     ,,,,,,};
     f(arr, N);
 }
class Solution {
    public int findDuplicate(int[] nums) {
        // Find the intersection point of the two runners.
        ];
        ];
        do {
            tortoise = nums[tortoise];
            hare = nums[nums[hare]];
        } while (tortoise != hare);

        // Find the "entrance" to the cycle.
        ];
        int ptr2 = tortoise;
        while (ptr1 != ptr2) {
            ptr1 = nums[ptr1];
            ptr2 = nums[ptr2];
        }

        return ptr1;
    }
}

287find-the-duplicate-number的更多相关文章

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

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

  2. 287. Find the Duplicate Number hard

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

  3. Leetcode Find the Duplicate Number

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

  4. Find the Duplicate Number

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

  5. LeetCode——Find the Duplicate Number

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

  6. 287. Find the Duplicate Number

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

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

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

  8. Find the Duplicate Number 解答

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

  9. LeetCode 287. Find the Duplicate Number (找到重复的数字)

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

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

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

随机推荐

  1. ansible软件2

    常用软件安装及使用目录  ansible使用1 第1章 copy模块 1.1 创建文件及写入内容 1. [root@m01 scripts]# ansible oldboy -m copy -a &q ...

  2. pairwork(黄敬博12061156和黄伟龙12061172)

    结对编程: 结对编程的优缺点: 优点: 1.相互督促,共同为了完成目标而努力: 2.节省时间,通过将疑难问题分开解决,共同讨论,实现了更高效的时间利用率: 3.能力互补,提高代码的质量,同时也提高了测 ...

  3. 现代软件工程构建之法 前五章阅读感想&困惑

    第一章 第一节 新时代中国的IT产业市场规则不规范,书中提到社会上有个别软件公司的软件一定要卸载别家公司的软件才能运行,我这里感到疑惑---————是不是说如果 一间软件公司他能做出一个像微软操作系统 ...

  4. python learning OOP2.py

    class Student(object): pass s = Student() s.name = 'Chang' # 给一个实例动态绑定一个属性 print(s.name) def set_age ...

  5. Java自学基础用法

    在慕课上面简单学习了一下java语言的用法 简单的用法总结记录一下. 代码(学习输入,输出): package hello; import java.util.Scanner; public clas ...

  6. redux相关专业名词及函数提要

    redux: 用来管理react app 状态(state)的一个架构. store: 通过createStore()创建,用来存放state,与react app是完全分离的.createStore ...

  7. react 组件构建设计

    项目设计中,可以从顶层React元素开始,然后实现它的子组件,自顶向下来构建组件的层级组件的写法:1.引入依赖模块2.定义React组件3.作为模块导出React组件4.子组件更新父组件的机制5.父组 ...

  8. 1105 C程序的推导过程

  9. [转帖]ssd固态硬盘的Trim命令是什么?

    ssd固态硬盘的Trim命令是什么?  收藏 分享 邀请 许多用户朋友在购买SSD的时候都会特别强调Trim,不过Trim是什么?做什么用的?   什么是Trim?   Trim指令也叫disable ...

  10. Java并发编程之线程生命周期、守护线程、优先级、关闭和join、sleep、yield、interrupt

    Java并发编程中,其中一个难点是对线程生命周期的理解,和多种线程控制方法.线程沟通方法的灵活运用.这些方法和概念之间彼此联系紧密,共同构成了Java并发编程基石之一. Java线程的生命周期 Jav ...