Skip to content

Commit fd8f57e

Browse files
committed
feat: solve missing number
1 parent 931ef0f commit fd8f57e

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

missing-number/GangBean.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public int missingNumber(int[] nums) {
3+
/**
4+
1. understanding
5+
- array nums, n distinct numbers in range [0, n]
6+
- find missing number
7+
2. strategy
8+
- you can calculate the sum of range [0, n]: n(n+1)/2 ... (1)
9+
- and the sum of nums ... (2)
10+
- and then extract (2) from (1) = (missing value) what we want.
11+
3. complexity
12+
- time: O(N), N is the length of nums
13+
- space: O(1)
14+
*/
15+
int N = nums.length;
16+
return N*(N+1)/2 - Arrays.stream(nums).sum();
17+
}
18+
}
19+

0 commit comments

Comments
 (0)