*LeetCode-Find the Duplicate Number

binary search但是search的是 在 start-end之中 有多少数小于mid 假如有多于mid个数字 说明start-mid之中有dup。else dup在mid + 1到end之中

public class Solution {
    public int findDuplicate(int[] nums) {
        int start = 1;
        int end = nums.length - 1;
        while ( start < end ){
            int mid = ( start + end ) / 2;
            int count = 0;
            for ( int i = 0; i < nums.length; i ++ ){
                if ( nums[i] <= mid )
                    count ++;
            }
            if ( count > mid )
                end = mid;
            else 
                start = mid + 1;
        }
        return start;
    }
}


你可能感兴趣的:(*LeetCode-Find the Duplicate Number)