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

  • 相关阅读:
    作业第十六周
    web、app、小程序测试异同点
    接口测试用例演进
    Python中“if __name__=='__main__':”
    iPhone发布内测程序的方法
    我的2020
    python 参数笔记 --> 位置参数 关键字参数 命名参数 形式参数 默认参数 可变参数 可选参数 位置顺序
    uwsgi 热部署 热启动 热更新
    Supervisor 进程管理工具 笔记
    spring Boot----注解驱动开发
  • 原文地址:https://www.cnblogs.com/lz87/p/7217521.html
Copyright © 2011-2022 走看看