287find-the-duplicate-number
某视面试官问了一道这样的题,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的更多相关文章
- [LeetCode] Find the Duplicate Number 寻找重复数
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...
- 287. Find the Duplicate Number hard
287. Find the Duplicate Number hard http://www.cnblogs.com/grandyang/p/4843654.html 51. N-Queens h ...
- Leetcode Find the Duplicate Number
最容易想到的思路是新开一个长度为n的全零list p[1~n].依次从nums里读出数据,假设读出的是4, 就将p[4]从零改成1.如果发现已经是1了,那么这个4就已经出现过了,所以他就是重复的那个数 ...
- Find the Duplicate Number
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...
- LeetCode——Find the Duplicate Number
Description: Given an array nums containing n + 1 integers where each integer is between 1 and n (in ...
- 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 解题思路
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...
- Find the Duplicate Number 解答
Question Given an array nums containing n + 1 integers where each integer is between 1 and n (inclus ...
- LeetCode 287. Find the Duplicate Number (找到重复的数字)
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...
- [Swift]LeetCode287. 寻找重复数 | Find the Duplicate Number
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...
随机推荐
- Python函数式编程中map()、reduce()和filter()函数的用法
Python中map().reduce()和filter()三个函数均是应用于序列的内置函数,分别对序列进行遍历.递归计算以及过滤操作.这三个内置函数在实际使用过程中常常和“行内函数”lambda函数 ...
- 作业 20181127-3 互评Beta版本
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2448 组名:可以低头,但没必要 组长:付佳 组员:张俊余 李文涛 孙赛佳 ...
- OO第四阶段总结
一.测试与正确性论证的区别 从哲学的角度来说,正确性论证与测试的关系就像理论与实践的关系一样. 使用测试的方法检验程序正确性确实是一个非常方便可行且广泛运用的方法.可以通过几个简单或复杂的测试样例,迅 ...
- 26_多线程_第26天(Thread、线程创建、线程池)_讲义
今日内容介绍 1.多线程 2.线程池 01进程概念 A:进程概念 a:进程:进程指正在运行的程序.确切的来说,当一个程序进入内存运行, 即变成一个进程,进程是处于运行过程中的程序,并且具有一定独立功能 ...
- express框架结合ejs模板引擎使用
我们在项目里建立一个views文件夹(必须),如果你不想使用views文件夹的话需要调用app.set("views","自定义文件夹名"),然后在里面建立一个 ...
- angularJS1笔记-(7)-控制器的合理使用(显示和隐式的依赖注入)
AngularJS依赖注入 1.隐式注入:不需要开发人员干预,angularJS自动根据参数的名称识别和注入数据 app.controller("myCtrl".function( ...
- js+Canvas 利用js 实现浏览器保存图片到本地
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 我所理解的Delphi中的数组类型
数组可以使Object Pascal所拥有的任何数据类型,数组是一些数值的简单集合. var MyArray: ..] of Integer; { 声明一个数组包括5个整数数值} b ...
- 关于css伪类
p:nth-child(2){} !选择所有p元素的第二个子元素: p:nth-of-type(2) !选择所有p元素第二个为p的子元素(是选择第二个类型为p的元素 而不是第二个子集为p的元素)
- [转帖]AMD、英特尔为何争相走向胶水多核处理器?真相在此
AMD.英特尔为何争相走向胶水多核处理器?真相在此 胶水多核到底好不好?这个事不是简单一句话能说明的,今天的超能课堂里我们就来聊聊MCM胶水多核技术的过去及未来. 作者:孟宪瑞来源:超能网|2018- ...