Leetcode - 287. Find the Duplicate Number
2016-03-05 09:03
363 查看
Problem Description:
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.
Analysis :
1. Binary Search O(nlogn) solution
Find the mid in the integer range, traverse the nums count the digit not greater than the mid.
if (count <= mid) the dups digit occurs in the right half;
else the dups in the left half.
Start from the end;
Another tricky solution using O(1) space, Here is the Link
4.
We can count the sum of each 32 bits separately for the given array (stored in “b” variable) and for the array [1, 2, 3, …, n] (stored in “a” variable). If “b” is greater than “a”, it means that duplicated number has 1 at the current bit position (otherwise, “b” couldn’t be greater than “a”).
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.
Analysis :
1. Binary Search O(nlogn) solution
Find the mid in the integer range, traverse the nums count the digit not greater than the mid.
if (count <= mid) the dups digit occurs in the right half;
else the dups in the left half.
int findDuplicate(vector<int>& nums) { int n = nums.size(); int l = 0, r = n - 1; while (l < r) { int m = l + ((r - l) >> 1); int count = 0; for (int i = 0; i < n; ++i) { if (nums[i] <= m) count ++; } if (count <= m ) l = m + 1; else r = m; } return l; }``` 2. Floyd's loop detection; if the size of nums is n; so the index range from [0...n - 1], the digit [1...n - 1]; We can define a function from the set {0,...n - 1} onto itself. f(i) = A [i]; so the duplicated value corresponds to a pair of indices i != j, such that f(i) = f(j), Our challenge ,therefore, is to find the pair(i, j). because the f(0) != 0,it can't be in the cycle, so we can start from nums[0]. if we want to start from the end f(n - 1) might equal with n - 1, so we set the f(n) = A - 1; Start from the 0; ``` int findDuplicate(vector<int>& nums) { int n = nums.size(); if(n <= 1) return -1; int slow = nums[0], fast = nums[nums[0]]; while (slow != fast) { slow = nums[slow]; fast = nums[nums[fast]]; } fast = 0; while (fast != slow) { fast = nums[fast]; slow = nums[slow]; } return slow; }
Start from the end;
int findDuplicate(vector<int>& nums) { int n = nums.size(), slow = n - 1, fast = n - 1; do { slow = nums[slow] - 1; fast = nums[nums[fast] - 1] - 1; } while (slow != fast); fast = n - 1; do { slow = nums[slow] - 1; fast = nums[fast] - 1; } while (slow != fast); return slow + 1; }
Another tricky solution using O(1) space, Here is the Link
int findDuplicate(vector<int>& nums) { int n = -1; for(int i = 0; i < nums.size(); i++) { n = abs(nums[i]); if(nums < 0) { break; } else { nums *= -1; } } // for(int j = 0; j < nums.size(); j++) { // nums[j] = std::abs(nums[j]); // } return n; }
4.
We can count the sum of each 32 bits separately for the given array (stored in “b” variable) and for the array [1, 2, 3, …, n] (stored in “a” variable). If “b” is greater than “a”, it means that duplicated number has 1 at the current bit position (otherwise, “b” couldn’t be greater than “a”).
int findDuplicate(vector<int>& nums) { int n = nums.size() - 1, res = 0; for (int p = 0; p < 32; ++p) { int bit = (1 << p), a = 0, b = 0; for (int i = 0; i <= n; ++i) { if (i > 0 && (i & bit) > 0) ++a; if ((nums[i] & bit) > 0) ++b; } if (b > a) res += bit; } return res; }
相关文章推荐
- [R语言画图]气泡图symbols
- iOS学习—UIFont字体
- cout 堆栈,operator<< 运算符重载输出问题
- 98%的人没解出的德国面试逻辑题
- C# for循环 循环中先求长度还是使用list.Count,哪个效率高
- CoreData简单使用
- python list and tuple
- 01.2异步服务IntentService
- 二叉树的深度 (递归与非递归方法整理)
- [WebGL入门]十九,遮挡剔除和深度測试
- 【转】phoneME Project Descrpition
- 0.v0 昨天和朋友讨论的美图2手机主题包匮乏的问题
- 二维指针控制指向NULL的一维指针
- 详解Java编程中Annotation注解对象的使用方法
- 获取含天气信息的JSON数据
- java 中static修饰变量的初始值
- EVC4.0编译错误:LNK2005
- 值得一提:关于 HDFS 的 file size 和 block size
- 滑动窗口协议
- 关于《海量用户积分算法探讨》的读后总结和扩展