某视面试官问了一道这样的题,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. export命令详解

    基础命令学习目录首页 export 的基本作用就是将父shell中的局部变量设置为环境变量,使得该变量可以在子shell中使用.下面设置两种情景对export进行原理解析. 情景  1. 有一个名为m ...

  2. 使用OpenCV通过摄像头捕获实时视频并探测人脸

    在Opencv初接触,图片的基本操作这篇手记中,我介绍了一些图片的基本操作,视频可以看作是一帧一帧的图片,因此图片操作其实是视频操作的基础,这篇手记就来讲讲OpenCV中的视频操作,并实现一个用笔记本 ...

  3. lambda----jdk8重头戏

    简介(译者注:虽然看着很先进,其实Lambda表达式的本质只是一个"语法糖",由编译器推断并帮你转换包装为常规的代码,因此你可以使用更少的代码来实现同样的功能.本人建议不要乱用,因 ...

  4. MAX值-单元测试

    #include<iostream> using namespace std; int Largest(int list[], int length); // list[]:求最大值的函数 ...

  5. 1001.A+B Format (20) 解题

    代码入口(https://github.com/NSDie/object-oriented) 这题的解题思路我有两个: 第一个是两个数字相加然后判断位数,因为题目限制了范围1000000的绝对值以内嘛 ...

  6. 乱码之UTF-8 &GBK

    在提交JSP时对于乱码问题,首先我们要搞清楚为什么会出现乱码? 看JSP的头文件:<%@ page contentType="text/html;charset=UTF-8" ...

  7. Python对list去重

    Python对list去重 方法一 新建新的列表,利用not in命令去重.这种方法看起来不够简便,但是保留了原列表中的顺序.代码如下: list1 = [1,2,3,4,1,1,2,5,4,3] l ...

  8. 用户数以及psp

    小组名称:好好学习 小组成员:林莉  王东涵   胡丽娜   宫丽君 项目名称: 记账本 alpha发布48小时以后用户数如何,是否达到预期目标,为什么,是否需要改进,如何改进(或理性估算). 首先我 ...

  9. python调用docker API(CentOS6.5)

    一 环境背景 python-2.7.8 docker 版本 1.15 (*yum安装为1.14版本,需升级为1.15,详见后续步骤) 二 获取Docker容器指标[指标可行性分析见笔记:] CPU : ...

  10. 原生Ajax函数

    前言 在日常工作中,我经常使用Jquery的Ajax来获取接口数据.这几天有一个的官网要制作,由于网站比较小,有一些与服务器通信的接口处理,并没有涉及到复杂的交互功能.为了可以减少加载Jquery库的 ...