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 03bcdeb commit a31c5f6Copy full SHA for a31c5f6
missing-number/TonyKim9401.java
@@ -1,13 +1,15 @@
1
-// TC: O(n log n)
+// TC: O(n)
2
+// -> add all nums into set
3
// SC: O(n)
4
+// -> set contains all nums' elements
5
class Solution {
6
public int missingNumber(int[] nums) {
- Arrays.sort(nums);
- int idx = 1;
7
- int n = nums.length;
8
- for (; idx < n; idx++) {
9
- if (nums[idx] - 1 != nums[idx-1]) return nums[idx] - 1;
10
- }
11
- return nums[idx-1] == n ? 0 : n;
+ Set<Integer> set = new HashSet<>();
+ for (int num : nums) set.add(num);
+
+ int output = 0;
+ while (set.contains(output)) output += 1;
12
13
+ return output;
14
}
15
0 commit comments