-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
31 lines (28 loc) · 802 Bytes
/
Solution.java
File metadata and controls
31 lines (28 loc) · 802 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
package missingnumber;
/**
* 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?
*/
class Solution {
int missingNumber(int[] nums) {
int total = 0;
for(int i: nums) total += i;
int len = nums.length;
int summation = len*(len+1)/2;
return summation - total;
}
}
/*
Runtime: 0 ms, faster than 100.00% of Java online submissions for Missing Number.
Memory Usage: 42.8 MB, less than 5.26% of Java online submissions for Missing Number.
*/