-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMissNumber.java
More file actions
35 lines (30 loc) · 895 Bytes
/
MissNumber.java
File metadata and controls
35 lines (30 loc) · 895 Bytes
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
package leetcode.array;
import java.util.Arrays;
/**
* @ClassName MissNumberLcci
* @Description 消失的数字 https://leetcode-cn.com/problems/missing-number-lcci/
* @Author changxuan
* @Date 2020/7/31 下午10:15
**/
public class MissNumber {
/**
* 另一种思路: 既然是连续的数字,可以用不缺数字的总和减去数字的实际总和
*
*/
public static void main(String[] args) {
int[] arrays = {9,6,4,2,3,5,7,0,1};
System.out.println(missingNumber(arrays));
}
public static int missingNumber(int[] nums) {
int[] index = new int[nums.length+1];
Arrays.fill(index, -1);
for (int i = 0; i < nums.length; i++){
index[nums[i]] = 0;
}
for (int j = 0; j < index.length; j++){
if (index[j] == -1)
return j;
}
return 0;
}
}