讨论/《画解剑指 Offer》 - 剑指 Offer 53 - II. 0~n-1中缺失的数字 - 解决方案/
《画解剑指 Offer》 - 剑指 Offer 53 - II. 0~n-1中缺失的数字 - 解决方案
共 5 个回复

采用加法来进行完成的

numss = [i for i in range(len(nums)+1)]
sum1 = sum(numss)
sum2 = sum(nums)
return sum1-sum2]

3
public int missingNumber(int[] nums) {//0~n-1 中缺失的数字
        int left =0;
        int right = nums.length-1;
        while(left<=right){
            int mid = (left+right)/2;
            if(nums[mid] == mid){
                left =mid+1;
            }else {
                right =mid-1;
            }
        }
        return left;
class Solution {
public:
    int missingNumber(vector<int>& nums) {
        int left = 0,right = nums.size() - 1;
        while(left <= right){
            int mid = (left + right)/2;
            if(nums[mid] == mid){
                left = mid + 1;
            }
            else{
                right = mid - 1;
            }
        }    
        return left;
    }
};

算法小白,用了个笨方法

 public int missingNumber(int[] nums) {
         int[] newArr= new int[nums.length];
        newArr[0] = 0;
        if (nums[0] != 0){
            return 0;
        }
        for (int i = 1; i < nums.length; i++) {
            newArr[i] = newArr[i-1] +1 ;
        }
        for (int i = 0; i <= newArr.length-1; i++) {
            if (newArr[i] != nums[i]){
                return newArr[i];
            }else if(Arrays.equals(newArr,nums)){
                return newArr[newArr.length-1] +1;
            }
        }
        return 0;
    }

用异或:

public int missingNumber(int[] nums) {
    int n = nums.length + 1;
    int result = 0;
    for (int i = 0; i < nums.length; i++) {
        result = result ^ (i+1) ^ nums[i];
    }

    return result;
}