-
Notifications
You must be signed in to change notification settings - Fork 0
/
268. Missing Number.java
41 lines (34 loc) · 1.04 KB
/
268. Missing Number.java
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
40
41
/**
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?
Solution Analysis:
Runtime: 0 ms, faster than 100.00% of Java online submissions for Missing Number.
Memory Usage: 39 MB, less than 100.00% of Java online submissions for Missing Number.
**/
class Solution {
public int missingNumber(int[] nums) {
int sum =0;
for(int i =0;i<nums.length;i++)
sum +=nums[i];
return (nums.length)*(nums.length+1)/2 - sum;
}
}
class Solution {
public int missingNumber(int[] nums) {
int countFreq[] = new int[nums.length+1];
for(int i =0;i<nums.length;i++)
countFreq[nums[i]]++;
for(int j =0;j<countFreq.length;j++){
if(countFreq[j] ==0)
return j;
}
return -1;
}
}