-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMissingNumber.java
More file actions
58 lines (54 loc) · 1.39 KB
/
MissingNumber.java
File metadata and controls
58 lines (54 loc) · 1.39 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package easy;
import java.util.Arrays;
/**
* ClassName: MissingNumber.java
* Author: chenyiAlone
* Create Time: 2019/5/16 10:39
* Description: No.268
* 思路:
* 一、
* 1. targetSum 为不缺数字的和,sum 为数组实际的和
* 2. return targetSum - Sum
* 二、
* 1. sort array
* 2. binary search 第一个num[i] 大于 i
*
*
* 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?
*
*/
public class MissingNumber {
public int missingNumberBySum(int[] nums) {
long len = nums.length;
long targetSum = 0, sum = 0;
for (int i = 0; i < len; i++) {
sum += nums[i];
targetSum += i + 1;
}
return (int)(targetSum - sum);
}
public int missingNumberByBinarySearch(int[] nums) {
Arrays.sort(nums);
int l = 0, r = nums.length -1;
while (l <= r) {
int mid = l + ((r - l) >> 1);
if (nums[mid] > mid) {
r = mid - 1;
} else {
l = mid + 1;
}
}
return l;
}
}