[LeetCode] Find the Duplicate Number

简介: There are mainly two solutions to solve this problem. The first one is very clever, using the idea of cycle detection, and runs in O(n) time.

There are mainly two solutions to solve this problem.

The first one is very clever, using the idea of cycle detection, and runs in O(n) time. You may refer to this post, whose code is rewritten below.

 1 class Solution {
 2 public:
 3     int findDuplicate(vector<int>& nums) {
 4         int n = nums.size(), slow = n - 1, fast = n - 1;
 5         do {
 6             slow = nums[slow] - 1;
 7             fast = nums[nums[fast] - 1] - 1;
 8         } while (slow != fast);
 9         fast = n - 1; 
10         do {
11             slow = nums[slow] - 1;
12             fast = nums[fast] - 1;
13         } while (slow != fast);
14         return slow + 1;
15     }
16 };

The second one uses binary search to search for possible numbers in ranges [1, n] and eliminate one half at each time. You may refer to this nice post for a good explanation. The code is rewritten below in C++.

 1 class Solution {
 2 public:
 3     int findDuplicate(vector<int>& nums) {
 4         int n = nums.size(), l = 1, r = n - 1;
 5         while (l < r) {
 6             int m = l + ((r - l) >> 1);
 7             int cnt = notGreaterThan(nums, m);
 8             if (cnt <= m) l = m + 1;
 9             else r = m;
10         } 
11         return l;
12     }
13 private:
14     int notGreaterThan(vector<int>& nums, int target) {
15         int cnt = 0;
16         for (int num : nums)
17             if (num <= target)
18                 cnt++;
19         return cnt;
20     }
21 };

 

目录
相关文章
|
Java
Leetcode 295. Find Median from Data Stream
在一个有序数组中找中位数,但需要支持再数组中添加新的元素。本来是有序里的,可以很轻易就查到中位数,但如果添加新数字后,不一定有序。如果先对数组排序,那代价就比较大了,每次排序时间复杂度O(n*log(n)),看discuss发现了一种很巧妙的解法,可以把添加数据的时间复杂度降低到O(log(n)) ,查询中位数O(1)。
51 0
|
算法
Leetcode 313. Super Ugly Number
题目翻译成中文是『超级丑数』,啥叫丑数?丑数就是素因子只有2,3,5的数,7 14 21不是丑数,因为他们都有7这个素数。 这里的超级丑数只是对丑数的一个扩展,超级丑数的素因子不再仅限于2 3 5,而是由题目给定一个素数数组。与朴素丑数算法相比,只是将素因子变了而已,解法还是和朴素丑数一致的。
100 1
|
5月前
|
存储 SQL 算法
LeetCode 题目 65:有效数字(Valid Number)【python】
LeetCode 题目 65:有效数字(Valid Number)【python】
|
6月前
|
存储 算法
【LeetCode力扣】单调栈解决Next Greater Number(下一个更大值)问题
【LeetCode力扣】单调栈解决Next Greater Number(下一个更大值)问题
45 0
Leetcode Find Minimum in Rotated Sorted Array 题解
对一个有序数组翻转, 就是随机取前K个数,移动到数组的后面,然后让你找出最小的那个数,注意,K有可能是0,也就是没有翻转。
49 0
|
存储
Leetcode Single Number II (面试题推荐)
给你一个整数数组,每个元素出现了三次,但只有一个元素出现了一次,让你找出这个数,要求线性的时间复杂度,不使用额外空间。
39 0
|
算法 Python
LeetCode 1160. 拼写单词 Find Words That Can Be Formed by Characters
LeetCode 1160. 拼写单词 Find Words That Can Be Formed by Characters
LeetCode 1160. 拼写单词 Find Words That Can Be Formed by Characters
|
JavaScript 索引
LeetCode 436. Find Right Interval
Given a set of intervals, for each of the interval i, check if there exists an interval j whose start point is bigger than or equal to the end point of the interval i, which can be called that j is on the "right" of i.
82 0
LeetCode 436. Find Right Interval
|
算法
LeetCode 414. Third Maximum Number
给定一个非空数组,返回此数组中第三大的数。如果不存在,则返回数组中最大的数。要求算法时间复杂度必须是O(n)。
95 0
LeetCode 414. Third Maximum Number
|
算法
LeetCode 405. Convert a Number to Hexadecimal
给定一个整数,编写一个算法将这个数转换为十六进制数。对于负整数,我们通常使用 补码运算 方法。
95 0
LeetCode 405. Convert a Number to Hexadecimal