首页 > 代码库 > 268. Missing Number

268. Missing Number

Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.

For example,
Given nums = [0, 1, 3] return 2.

121/145

思路:bit manipulation A^A=0 A^0=A. 

nums[i]属于0- i+1,他们中必有一个数是missing的,所以跟i+1异或之后肯定会留下最后0^A=A

public class Solution {    public int missingNumber(int[] nums) {    int len=nums.length;    for(int i=0;i<nums.length;i++){        len^=i;        len^=nums[i];    }    return len;    }}

 

268. Missing Number