-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMissingNumber_268.java
More file actions
39 lines (34 loc) · 1.44 KB
/
Copy pathMissingNumber_268.java
File metadata and controls
39 lines (34 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.leetcode.array;
/**
* Created by charles on 12/15/16.
* 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.
Note:
Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?
*/
public class MissingNumber_268 {
/**
* Thought:
* The basic idea is to use XOR operation. We all know that a^b^b =a,
* which means two xor operations with the same number will eliminate the number and reveal the original number.
In this solution, I apply XOR operation to both the index and value of the array. In a complete array with no missing numbers,
the index and value should be perfectly corresponding( nums[index] = index), so in a missing array, what left finally is the missing number.
*/
public int missingNumber(int[] nums) {
int res = 0;
for (int i = 0; i < nums.length; i++) {
System.out.println(" res : " + res);
res ^= i;
System.out.println(" res after i : " + res);
res ^= nums[i];
System.out.println(" res after nums[i]: " + res);
}
return res ^ nums.length;
}
public static void main(String[] args) {
MissingNumber_268 m = new MissingNumber_268();
int[] nums = {0,3,1};
System.out.println(m.missingNumber(nums));
}
}