forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMissingNumber.java
More file actions
36 lines (32 loc) · 728 Bytes
/
MissingNumber.java
File metadata and controls
36 lines (32 loc) · 728 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
36
package normal; /**
* @program JavaBooks
* @description: 268.缺失数字
* @author: mf
* @create: 2019/11/04 17:59
*/
/**
* 题目:https://leetcode-cn.com/problems/missing-number/
* 难度:easy
* 类型:数组
*/
/*
输入: [3,0,1]
输出: 2
输入: [9,6,4,2,3,5,7,0,1]
输出: 8
*/
public class MissingNumber {
public static void main(String[] args) {
int[] nums = {3, 0 , 1};
int[] nums1 = {9,6,4,2,3,5,7,0,1};
System.out.println(missingNumber(nums1));
}
private static int missingNumber(int[] nums) {
int ans = nums.length;
for (int i = 0; i < nums.length; i++) {
ans ^= nums[i];
ans ^= i;
}
return ans;
}
}