Find the Duplicate Number 解答
Question
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one.
Note:
- You must not modify the array (assume the array is read only).
- You must use only constant, O(1) extra space.
- Your runtime complexity should be less than
O(n2)
. - There is only one duplicate number in the array, but it could be repeated more than once.
Solution 1 -- Binary Search
题目要求时间复杂度小于O(n2),于是我们就想有没有O(n log n)或者O(n)的做法。一些排序算法是O(n log n),但是题目要求不能更改原序列且空间复杂度为O(1)。
Binary search的复杂度是O(log n),前提是排好序的数组。所以肯定不能用输入数组来进行二分查找。
这一题提供了一个思路是对可行解序列/集合进行二分查找。
由于题目中有明确的各个元素的取值范围,我们可以判断出解一定在[1, n]这个区间内。start = 1, end = n。对于每个mid值,我们计算等于mid的count和小于等于mid的count。
注意:
smallerCount 是和mid值比较。比如mid = 5,那么如果smallerCount <= 5,说明解一定不在[1,5]这个区间内。
public class Solution {
public int findDuplicate(int[] nums) {
int start = 1, end = nums.length - 1, mid;
while (start + 1 < end) {
mid = (end - start) / 2 + start;
int smallerMid = 0;
for (int i : nums) {
if (i <= mid) {
smallerMid++;
}
}
// Compare with mid
if (smallerMid <= mid) {
start = mid;
} else{
end = mid;
}
}
int countStart = 0;
for (int i : nums) {
if (i == start) {
countStart++;
}
}
if (countStart > 1) {
return start;
}
return end;
}
}
Solution 2
参考Discuss,发现有O(n)的解法
参考Linked List II,我们将输入的array也可看作是list,每个数组元素代表这个node的next
public class Solution {
public int findDuplicate(int[] nums) {
if (nums == null || nums.length < 2) {
return 0;
}
int slow = nums[0];
int fast = nums[slow];
while (fast != slow) {
slow = nums[slow];
fast = nums[nums[fast]];
}
fast = 0;
while (fast != slow) {
slow = nums[slow];
fast = nums[fast];
}
return slow;
}
}
Find 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 ...
- 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 ...
随机推荐
- poj2823:单调队列入门题
今天学习了一下单调队列这种数据结构,思想不是很难 参考资料:http://www.cnblogs.com/Jason-Damon/archive/2012/04/19/2457889.html 然后自 ...
- 第29讲 UI组件之 ListView与 BaseAdapter,SimpleAdapter
第29讲 UI组件之 ListView与 BaseAdapter,SimpleAdapter 1.BaseAdapter BaseAdapter是Android应用程序中经常用到的基础数据适配器,它的 ...
- Java IO 概述
输入和输出-数据源和目标媒介 术语“输入”和“输出”有时候会有一点让人疑惑.一个应用程序的输入往往是另一个应用程序的输出.那么OutputStream流到底是一个输出到目的地的流呢,还是一个产生输出的 ...
- 具体解说Android的图片下载框架UniversialImageLoader之磁盘缓存的扩展(二)
相对于第一篇来讲,这里讲的是磁盘缓存的延续.在这里我们主要是关注四个类.各自是DiskLruCache.LruDiskCache.StrictLineReader以及工具类Util. 接下来逐一的对它 ...
- Word文档分割总结
Word文档分割总结 方法: 1. word创建子文件实现文件分割 2. VBA实现 3. 网上分割合并的插件软件 一. word创建子文件实现文件分割 打开需要分割的文件 >> 视图 & ...
- C# 将窗口移动到指定位置
private void button1_Click(object sender, EventArgs e) { Frm f = new Frm(); f.StartPosition = FormSt ...
- datatable列操作
DataTable myDt =dt; //删除列 myDt.Columns.Remove("minArea"); myDt.Columns.Remove("max ...
- 利用“参数赋值”防范SQL注入漏洞攻击
<<年轻,无权享受>————送给每一个看到此文的同僚们 在这无精打采的炎夏 我躺在阳台上房东的旧沙发 回想几个月来遇到的问题 我不禁内心开始慌张喘着粗气 还有大把时间去打拼 没有到只 ...
- C# 温故而知新:Stream篇(—)
C# 温故而知新:Stream篇(—) 目录: 什么是Stream? 什么是字节序列? Stream的构造函数 Stream的重要属性及方法 Stream的示例 Stream异步读写 Stream 和 ...
- angularjs字符串插值($interpolate)
<!DOCTYPE html> <html lang="zh-CN" ng-app="app"> <head> <me ...