Skip to content

Commit 1deef50

Browse files
refactor 268
1 parent 1fe8081 commit 1deef50

File tree

1 file changed

+12
-16
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+12
-16
lines changed
Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
11
package com.fishercoder.solutions;
22

3-
/**Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.
4-
5-
For example,
6-
Given nums = [0, 1, 3] return 2.
7-
8-
Note:
9-
Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?*/
10-
113
public class _268 {
124

13-
/**we could take advantage of the array indices
14-
then a number xor with itself is zero, so after we xor the entire array with all of its indices, the missing number will show up.*/
15-
public int missingNumber(int[] nums) {
16-
int xor = 0;
17-
int i = 0;
18-
for (; i < nums.length; i++) {
19-
xor = xor ^ i ^ nums[i];
5+
public static class Solution1 {
6+
/**
7+
* we could take advantage of the array indices
8+
* then a number xor with itself is zero, so after we xor the entire array with all of its indices, the missing number will show up.
9+
*/
10+
public int missingNumber(int[] nums) {
11+
int xor = 0;
12+
int i = 0;
13+
for (; i < nums.length; i++) {
14+
xor = xor ^ i ^ nums[i];
15+
}
16+
return xor ^ i;
2017
}
21-
return xor ^ i;
2218
}
2319

2420
}

0 commit comments

Comments
 (0)