zoukankan      html  css  js  c++  java
  • [LeetCode 287] Find the Duplicate Number

    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.

     Notice
    1. You must not modify the array (assume the array is read only).
    2. You must use only constant, O(1) extra space.
    3. Your runtime complexity should be less than O(n^2).
    4. There is only one duplicate number in the array, but it could be repeated more than once.
    Example

    Given nums = [5,5,4,3,2,1] return 5
    Given nums = [5,4,4,3,2,1] return 4

     

    Solution 1. O(n * log n) runtime, O(1) space, Binary Search on the max possible value n

     

     1 public class Solution {
     2     public int findDuplicate(int[] nums) {
     3         if(nums == null || nums.length < 2){
     4             return -1;
     5         }
     6         int start = 1, end = nums.length - 1;
     7         while(start + 1 < end){
     8             int mid = start + (end - start) / 2;
     9             if(countSmallerAndEqual(mid, nums) <= mid){
    10                 start = mid;
    11             }
    12             else{
    13                 end = mid;
    14             }
    15         }
    16         if(countSmallerAndEqual(start, nums) <= start){
    17             return end;
    18         }
    19         return start;
    20     }
    21     private int countSmallerAndEqual(int mid, int[] nums){
    22         int cnt = 0;
    23         for(int i = 0; i < nums.length; i++){
    24             if(nums[i] <= mid){
    25                 cnt++;
    26             }
    27         }
    28         return cnt;
    29     }
    30 }

     

    Solution 2. O(n) runtime, O(1) extra space, Linked List cycle detection, two pointers, slow and fast pointers

     

     1 public class Solution {
     2     public int findDuplicate(int[] nums) {
     3         if(nums == null || nums.length < 2){
     4             return -1;
     5         }
     6         
     7         int slow = nums[0];
     8         int fast = nums[nums[0]];
     9         while(slow != fast){
    10             slow = nums[slow];
    11             fast = nums[nums[fast]];
    12         }
    13         fast = 0;
    14         while(slow != fast){
    15             slow = nums[slow];
    16             fast = nums[fast];
    17         }
    18         return slow;
    19     }
    20 }

     

    Related Problems 

    Find the Missing Number

    Find the Missing Number II

    First Missing Positive

    Single Number

    Linked List Cycle II

  • 相关阅读:
    第28月第23天 lineFragmentPadding
    第28月第22天 iOS动态库
    第28月第21天 记事本Unicode 游戏编程中的人工智能技术
    第28月第11天 vim -b
    第28月第10天 iOS动态库
    第28月第8天
    第28月第7天 本地摄像头方向
    第28月第5天 uibutton交换方法
    第28月第4天 __bridge_transfer
    python __getattr__ __setattr__
  • 原文地址:https://www.cnblogs.com/lz87/p/7217521.html
Copyright © 2011-2022 走看看