We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 931ef0f commit fd8f57eCopy full SHA for fd8f57e
missing-number/GangBean.java
@@ -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