-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathMissingNumber268.java
More file actions
39 lines (34 loc) · 877 Bytes
/
MissingNumber268.java
File metadata and controls
39 lines (34 loc) · 877 Bytes
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
/**
* Given an array containing n distinct numbers taken from 0, 1, 2, ..., n,
* find the one that is missing from the array.
*
* Example 1:
* Input: [3,0,1]
* Output: 2
*
* Example 2:
* Input: [9,6,4,2,3,5,7,0,1]
* Output: 8
*
* Note:
* Your algorithm should run in linear runtime complexity.
* Could you implement it using only constant extra space complexity?
*/
public class MissingNumber268 {
public int missingNumber(int[] nums) {
int sum = 0;
int N = nums.length;
for (int n: nums) sum += n;
return N * (N + 1) / 2 - sum;
}
/**
* https://leetcode.com/problems/missing-number/solution/
*/
public int missingNumber2(int[] nums) {
int missing = nums.length;
for (int i = 0; i < nums.length; i++) {
missing ^= i ^ nums[i];
}
return missing;
}
}