Skip to content

Commit 9c90af2

Browse files
committed
add MissingNumber
1 parent 01f63bd commit 9c90af2

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

hi-leetcode/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
| 0134 | [Gas Station](https://leetcode.com/problems/gas-station/) | [Java](./src/main/java/me/meet/leetcode/medium/GasStation.java) | Medium |
3535
| 0150 | [Evaluate Reverse Polish Notation](https://leetcode.com/problems/evaluate-reverse-polish-notation/) | [Java](./src/main/java/me/meet/leetcode/medium/EvaluateReversePolishNotation.java) | Medium |
3636
| 0241 | [Different Ways To Add Parentheses](https://leetcode.com/problems/different-ways-to-add-parentheses/) | [Java](./src/main/java/me/meet/leetcode/medium/DifferentWaysToAddParentheses.java) | Medium |
37+
| 0268 | [Missing Number](https://leetcode.com/problems/missing-number/) | [Java](./src/main/java/me/meet/leetcode/medium/MissingNumber.java) | Medium |
3738
| 0442 | [Find All Duplicates In An Array](https://leetcode.com/problems/find-all-duplicates-in-an-array/) | [Java](./src/main/java/me/meet/leetcode/medium/FindAllDuplicatesInAnArray.java) | Medium |
3839
| 0445 | [Add Two Numbers II](https://leetcode.com/problems/add-two-numbers-ii/) | [Java](./src/main/java/me/meet/leetcode/medium/AddTwoNumbersII.java) | Medium |
3940
| 0450 | [Delete Node In BST](https://leetcode.com/problems/delete-node-in-a-bst/) | [Java](./src/main/java/me/meet/leetcode/medium/DeleteNodeInBST.java) | Medium |
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package me.meet.leetcode.medium;
2+
3+
public final class MissingNumber {
4+
private MissingNumber() {}
5+
6+
/**
7+
* Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.
8+
*
9+
* For example,
10+
* Given nums = [0, 1, 3] return 2.
11+
*
12+
* Note:
13+
* Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?
14+
*
15+
*
16+
* 题意:
17+
* 这道题给我们n个数字,是0到n之间的数但是有一个数字去掉了,让我们寻找这个数字,要求线性的时间复杂度和常数级的空间复杂度。
18+
*
19+
* 那么最直观的一个方法是用等差数列的求和公式求出0到n之间所有的数字之和,然后再遍历数组算出给定数字的累积和,然后做减法,差值就是丢失的那个数字
20+
*/
21+
static int getMissingNumber(int[] arr) {
22+
int sum = 0, n = arr.length;
23+
for (int i = 0; i < n; i++) {
24+
sum += arr[i];
25+
}
26+
27+
return n*(n+1)/2 - sum;
28+
}
29+
30+
/**
31+
* 这题还有一种解法,使用位操作Bit Manipulation来解的,用到了异或操作的特性
32+
* 那么思路是既然0到n之间少了一个数,我们将这个少了一个数的数组合0到n之间完整的数组异或一下,那么相同的数字都变为0了,剩下的就是少了的那个数字了
33+
*/
34+
static int getMissingNumberXOR(int[] arr) {
35+
int res = 0, n = arr.length;
36+
for (int i = 0; i < n; i++) {
37+
res ^= (i+1)^arr[i];
38+
}
39+
return res;
40+
}
41+
42+
/**
43+
* 1. 排序
44+
* 2. 二分查找到 丢失到数字
45+
*/
46+
47+
private static void testGetMissingNumber() {
48+
int[] arr = new int[]{0,1,2,3,4,6,7,8,9};
49+
50+
System.out.println(getMissingNumber(arr));
51+
System.out.println(getMissingNumberXOR(arr));
52+
}
53+
54+
public static void main(String[] args) {
55+
testGetMissingNumber();
56+
}
57+
}

0 commit comments

Comments
 (0)