LeetCode Find the Duplicate Number 找重复出现的数(技巧)
题意:
有一个含有n+1个元素的数组,元素值是在1~n之间的整数,请找出其中出现超过1次的数。(保证仅有1个出现次数是超过1的数)
思路:
方法一:O(nlogn)。根据鸽笼原理及题意,每次如果<=k的数超过了k个,那么答案必定在[1,k]。可以用二分枚举答案来解决。
bool left(vector<int>& nums,int tar)//是否在左边
{
int cnt=;
for(int i=; i<nums.size(); i++)
if(nums[i]<=tar)
cnt++;
return cnt>tar;
} int findDuplicate(vector<int>& nums)
{
int L=, R=nums.size()-;
while(L<R)
{
int mid=R-(R-L+)/;
if( left(nums,mid)==true ) R=mid;
else L=mid+;
}
return R;
}
AC代码
方法二:O(n)。将数组nums看成是一个链表,next[i]表示点i的后继(0也是一个点,因为0也是下标)。根据题意,此链表必定有且仅有一个简单环存在,这样就类似于Linked List Cycle II ,只是会多余出部分的链,但是这不会影响到这个模型,从0点出发依然存在这样的一个模型,只是环的接口处不会是0而已。要注意两个指针的起始位置,必须保证fast=2*slow。
class Solution {
public:
int findDuplicate(vector<int>& nums)
{
int slow=, fast=nums[];
while(fast!=slow)
{
slow=nums[slow];
fast=nums[nums[fast]];
}
fast=;
slow=nums[slow];
while(fast!=slow)
{
slow=nums[slow];
fast=nums[fast];
}
return fast;
}
};
AC代码
LeetCode Find the Duplicate Number 找重复出现的数(技巧)的更多相关文章
- [LeetCode] 219. Contains Duplicate II 包含重复元素 II
Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...
- [LeetCode] 220. Contains Duplicate III 包含重复元素 III
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
- 287. Find the Duplicate Number 找出数组中的重复数字
[抄题]: Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive ...
- [LeetCode] 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 ...
- Leetcode Find the Duplicate Number
最容易想到的思路是新开一个长度为n的全零list p[1~n].依次从nums里读出数据,假设读出的是4, 就将p[4]从零改成1.如果发现已经是1了,那么这个4就已经出现过了,所以他就是重复的那个数 ...
- [CareerCup] 10.6 Find Duplicate URLs 找重复的URL链接
10.6 You have 10 billion URLs. How do you detect the duplicate documents? In this case, assume that ...
- LeetCode 414. Third Maximum Number (第三大的数)
Given a non-empty array of integers, return the third maximum number in this array. If it does not e ...
- LeetCode 217. Contains Duplicate (包含重复项)
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
随机推荐
- WPF RichTextBox读取存储文本的方法和常用属性
1. 取得已被选中的内容: (1)使用 RichTextBox.Document.Selection属性(2)访问RichTextBox.Document.Blocks属性的“blocks”中的Tex ...
- javascript密码强度验证!
//CharMode函数 //测试某个字符是属于哪一类 function CharMode(iN) { if (iN>=48 && iN <=57) //数字 return ...
- Matrix-Tree定理
感觉又学到了一个利器! 感谢Vfleaking神犇,传送门 http://vfleaking.blog.163.com/blog/static/1748076342013112523651955/ ...
- Oracle练习题20~33
20.查询score中选学多门课程的同学中分数为非最高分成绩的记录. 21. 查询成绩高于学号为“109”.课程号为“3-105”的成绩的所有记录. 22.查询和学号为108的同学同年出生的所有学生的 ...
- POJ 2159 Ancient Cipher 难度:0
题目链接:http://poj.org/problem?id=2159 #include <cstring> #include <cstdio> #include <cc ...
- Hibernate中的集合映射
1.定义实体 public class User { private int userId; private String userName; private Set<String> ad ...
- Maven 玩 github上的项目
第一步,使用maven创建了一个项目"helloworld",cmd命令如下: @echo offecho [INFO] Generating project in ./gener ...
- CSS-长图水平居中
场景:客户方给我了一张1920px的长图给我,然后告诉我在屏幕不到1920px时候,屏幕显示图片的中心位置,左右边缘可以不要. 当屏幕小于1000px的时候,图片显示中心部分1000px的图片,且可以 ...
- 如何利用SVN合并代码
一. 背景 平时在进行开发时,一般都会有多版本同时进行,包括项目版本.周版本.紧急版本等,当某一个版本具备上线条件后,需要在上一个已发布的版本基础上进行发布,才能够避免出现版本相互覆盖,因此 ...
- 压力测试工具——Galting
为什么要写Gatling呢?网上已经有一些介绍Gatling的好文章了,比如两位TW同事的文章,可以看这里(我知道Gatling也是因为这位作者介绍的),还有这里.主要是因为最近在使用Gatling做 ...