点名 (点名)

 

思路:

// @Title: 点名 (点名)
// @Author: qisiii
// @Date: 2022-03-01 21:45:07
// @Runtime: 0 ms
// @Memory: 42.3 MB
// @comment: 
// @flag: 
class Solution {
    public int missingNumber(int[] nums) {
        int left=0;
        int right=nums.length-1;
        if(nums[right]==right){
            return right+1;
        }
        if(right==0){
            return 0;
        }
        while(left<=right){
            int mid=left+(right-left)/2;
            if(nums[mid]==mid){
                left=mid+1;
            }else if(nums[mid]>mid&&((mid>0&&nums[mid-1]==mid-1)||mid==0)){
                return mid;
            }else{
                right=mid-1;
            }
        }
        return -1;
    }
}
Licensed under CC BY-NC-SA 4.0
最后更新于 2024-10-18