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 ...
随机推荐
- [zabbix] zabbix检测mysql主从状态
环境说明: zabbix-proxy 172.16.2.95(zabbix-server同理) zabbix-agent111 172.16.2.111 mysql从机 1.mysql从机添加用户权限 ...
- FFmpeg简单转码程序--视频剪辑
学习了雷神的文章,慕斯人分享精神,感其英年而逝,不胜唏嘘.他有分享一个转码程序<最简单的基于FFMPEG的转码程序>其中使用了filter(参考了ffmpeg.c中的流程),他曾说想再编写 ...
- decode 函数及其用法
http://blog.csdn.net/oscar999/article/details/18399177
- 读取classpath配置文件的方法
http://www.cnblogs.com/sprinng/p/5622600.html
- Java程序设计实验 实验五
课程:Java程序设计实验 班级:1353 姓名:符余佳源 学号:20135321 成绩: 指导教师:娄嘉鹏 实验日期:2015. ...
- web07-jdbcBookStore
新建web项目,名字 新建servlet,名字CreateDBServlet 内容为: ---- 配置web.xml 数据库的URL.driveclass.user.passWord都写在web.xm ...
- golang string转json的一些坑
先带来点冷知识,不知道大家知不知道,反正我刚知道... 大佬们都知道怎么在string中给string类型赋值带双引号的字符串,没错就是用反斜杠,如下: msg := "{\"na ...
- 结对项目-小学生四则运算系统(GUI)
Coding克隆地址:https://git.coding.net/FrrLolix/CalGUI.git 伙伴博客:http://www.cnblogs.com/wangyy39/p/8763244 ...
- Unity控件ScrollView使用问题记录
Unity版本:5.6.2 控件Scroll View由4部分组成,如图: 1.含有Scroll Rect组件的根节点:Scroll View 2.含有Mask组件的节点:Viewport 3.所有内 ...
- Python开发【第五篇】迭代器、生成器、递归函数、二分法
阅读目录 一.迭代器 1. 迭代的概念 #迭代器即迭代的工具(自定义的函数),那什么是迭代呢? #迭代:指一个重复的过程,每次重复都可以称之为一次迭代,并且每一次重复的结果是下一个迭代的初始值(例如: ...