leetcode287 Find the Duplicate Number
思路:
转换成链表之后使用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的更多相关文章
- [Swift]LeetCode287. 寻找重复数 | 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 寻找重复数
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 ...
随机推荐
- [FJOI 2016] 神秘数
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4408 [算法] 首先考虑一组询问怎样做 : 将数组按升序排序 , 假设我们现在可以表 ...
- [SDOI2012]任务安排
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2726 [算法] 此题与POJ1180非常相似 但是 , 此题中的t值可能为负 , 这 ...
- 洛谷P4013数字梯形问题——网络流24题
题目:https://www.luogu.org/problemnew/show/P4013 最大费用最大流裸题: 注意:在第二种情况中,底层所有点连向汇点的边容量应该为inf,因为可以有多条路径结束 ...
- HDU acm1028 整数划分 递归问题(递推)
我们用递归+记忆化的方法来解决普通整数划分问题:定义 f(n,m)为将整数n划分为一系列整数之和,其中加数 最大不超过m. 得到下面的递推关系式: 当n==1 || m==1 只有一种划分,即 1 或 ...
- C语言指针入门知识
C语言指针往往是C语言学习过程中最困难的地方, 最近重新理解了一下C语言的指针知识, 在此整理一下, 如果有错误请留言指正. 对于刚入门的人来说, 指针涉及方方面面, 从简单的数组到结构体, 都会用到 ...
- 关于 jwTextFiled 的使用说明
我在些项目中多次对一些输入框做了相同的代码操作,终于有一天忍不住了,MD必须写一个小的框架解决这个搬砖的问题.经过一天的战斗,于是 jwTextFied 就默默的诞生了. 地址:https://git ...
- themeforest 免费模板
http://www.im286.com/thread-9550365-1-1.html 官网:http://themeforest.net/item/elena-responsive-email-t ...
- 微信小程序开发之页面数据绑定
js:Page( { data:{ parmer:"", //字符串参数 userinfo:{ userphone:"", ...
- 爬虫代码实现四:采用Hbase存储爬虫数据(1)
3.Hbase表设计: 1.窄表:列少行多,表中的每一行尽可能保持唯一. 2.宽表:列多行少,通过时间戳版本来进行区分取值. 窄表:比如说,这个表,rowkey由userid+时间+bbsid假设bb ...
- JAVA基础--JAVA API集合框架(其他集合类,集合原理)15
一.ArrayList介绍 1.ArrayList介绍 ArrayList它是List接口的真正的实现类.也是我们开发中真正需要使用集合容器对象. ArrayList类,它是List接口的实现.肯定拥 ...