题目

给出一个包含 0 .. N 中 N 个数的序列,找出0 .. N 中没有出现在序列中的那个数。

样例

N = 4 且序列为 [0, 1, 3] 时,缺失的数为2

注意

可以改变序列中数的位置。

挑战

在数组上原地完成,使用O(1)的额外空间和O(N)的时间。

解题

重新定义一个数组存放排序后的数,空间复杂度和时间复杂度都是O(N)

public class Solution {
    /**    
     * @param nums: an array of integers
     * @return: an integer
     */
    public int findMissing(int[] nums) {
        // write your code here
        boolean[] A = new boolean[nums.length +1];
        for(int i = 0;i<nums.length; i++){
            A[nums[i]] = true;
        }
        int n = 0;
        for(int i = 0;i< A.length ;i++){
            if(A[i] == false){
                n = i;
                break;
            }
        }
        
        return n;
    }
}
Java Code

总耗时: 1674 ms

class Solution:
    # @param nums: a list of integers
    # @return: an integer
    def findMissing(self, nums):
        # write your code here
        A = [False]*(len(nums) + 1)
        for a in nums:
            A[a] = True
        for i in range(len(A)):
            if A[i] == False:
                return i
Python Code

总耗时: 276 ms

在下面的挑战中,说可以在原始数组上面操作,如何在原始数组上面操作?空间复杂度并且是O(1) 

 i^i = 0 一个数自身的异或等于0

这个可以空间复杂可以是O(1),就有下面的代码了

public class Solution {
    /**    
     * @param nums: an array of integers
     * @return: an integer
     */
    public int findMissing(int[] nums) {
        // write your code here
        int res = 0;
        for( int i =0;i< nums.length ;i++){
            res = res ^ nums[i] ^ i;
        }
        res = res^(nums.length);
        return res;
    }
}
Java Code

总耗时: 1802 ms

class Solution:
    # @param nums: a list of integers
    # @return: an integer
    def findMissing(self, nums):
        # write your code here
        res = 0
        for i in range(len(nums)):
            res = res ^ i ^ nums[i]
        res ^= len(nums)
        return res 
Python Code

相关文章:

  • 2022-12-23
  • 2021-10-17
  • 2022-12-23
  • 2022-01-08
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-22
猜你喜欢
  • 2021-10-23
  • 2022-12-23
  • 2021-07-26
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-01
相关资源
相似解决方案