Return to school: Question 5-268. Lost Numbers (Hash + Bit Operation + Sum + Sort)

268. Missing Numbers

Given an array nums containing n numbers in [0, n], find the number in the range [0, n] that does not appear in the array.

进阶:

你能否实现线性时间复杂度、仅使用额外常数空间的算法解决此问题?
 

示例 1:

输入:nums = [3,0,1]
输出:2
解释:n = 3,因为有 3 个数字,所以所有的数字都在范围 [0,3] 内。2 是丢失的数字,因为它没有出现在 nums 中。
示例 2:

输入:nums = [0,1]
输出:2
解释:n = 2,因为有 2 个数字,所以所有的数字都在范围 [0,2] 内。2 是丢失的数字,因为它没有出现在 nums 中。
示例 3:

输入:nums = [9,6,4,2,3,5,7,0,1]
输出:8
解释:n = 9,因为有 9 个数字,所以所有的数字都在范围 [0,9] 内。8 是丢失的数字,因为它没有出现在 nums 中。
示例 4:

输入:nums = [0]
输出:1
解释:n = 1,因为有 1 个数字,所以所有的数字都在范围 [0,1] 内。1 是丢失的数字,因为它没有出现在 nums 中。
 

提示:

n == nums.length
1 <= n <= 104
0 <= nums[i] <= n
nums 中的所有数字都 独一无二

answer:

Method 1: Sort

The most obvious first is the sorting method, that is, after sorting the array, you can find it by traversing the array. The code is omitted.

Method two: hash table

Code:

int missingNumber(int* nums, int numsSize){
    
    
    int hash[10001]={
    
    0};
    for(int i=0;i<numsSize;i++)
    {
    
    
        hash[nums[i]]++;
    }
    for(int i=0;i<=numsSize;i++)
    {
    
    
        if(hash[i]!=1)
        {
    
    
            return i;
        }
    }
    return 1;
}

Method three: bit operation

The bit arithmetic code I wrote at the beginning is similar to the problem of finding a different method in Likou, that is, setting up an array cmp that does not lose numbers as a comparison array and nums array using the bitwise exclusive OR operator to compare, based on the same number After XOR twice, it becomes the original characteristics, and the last remaining is what you want.

Code:

int missingNumber(int* nums, int numsSize){
    
    
    int cmp[10001];
    for(int i=0;i<=numsSize;i++)
    {
    
    
        cmp[i]=i;
    }
    int res = 0;
    for(int i=0;i<numsSize;i++)
    {
    
    
        res=res^nums[i];
    }
    for(int i=0;i<=numsSize;i++)
    {
    
    
        res=res^cmp[i];
    }
    return res;
}

Of course, the above is more troublesome and can be optimized, that is, we find that there is no need to set up a separate comparison array. Because of the association between i and nums[i], we can do it in one step :

int missingNumber(int* nums, int numsSize){
    
    
    int res = 0;
    for(int i=0;i<numsSize;i++)
    {
    
    
        res=res^(nums[i]^i);//一次对两个进行异或
    }
    res=res^numsSize;//最后再补上一个循环中少的元素
    return res;
}

It can also be more optimized , that is, directly assign the elements to be complemented to res, that is, elements that are not calculated in the loop:

int missingNumber(int* nums, int numsSize){
    
    
    int res = numsSize;
    for(int i=0;i<numsSize;i++)
    {
    
    
        res=res^(nums[i]^i);
    }
    return res;
}

Method 4: Sum

The size of the number can be used to know.

Code:

int missingNumber(int* nums, int numsSize){
    
    
    int res = numsSize;
    for(int i=0;i<numsSize;i++)
    {
    
    
        res=res+i-nums[i];
    }
    return res;
}

Guess you like

Origin blog.csdn.net/xiangguang_fight/article/details/114401433