Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit eafaace

Browse files
committed
docs: 新增No268题解
1 parent 4a758f0 commit eafaace

File tree

2 files changed

+118
-73
lines changed

2 files changed

+118
-73
lines changed

leetcode刷题/README.md

Lines changed: 74 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,49 @@
11
# LootCode 刷题
22

3-
- [No.1 两数之和](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No1_two-sum.md)
4-
- [No.7 整数反转](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No7_reverse.md)
5-
- [No.8 字符串转换整数 (atoi)](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No8_my-atoi.md)
3+
- [No.1 两数之和](note/No1_two-sum.md)
4+
- [No.7 整数反转](note/No7_reverse.md)
5+
- [No.8 字符串转换整数 (atoi)](note/No8_my-atoi.md)
66
- [No.13 罗马数字转整数](note/No13_roman-to-int.md)
7-
- [No.14 最长公共前缀](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No14_longest-common-prefix.md)
8-
- [No.19 删除链表倒数第 n 个节点](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No19_remove-nth-from-end.md)
9-
- [No.20 有效的括号](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No20_is-valid.md)
10-
- [No.21 合并两个有序链表](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No21_merge-two-lists.md)
11-
- [No.26 从排序数组中删除重复项](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No26_remove-duplicates.md)
12-
- [No.36 有效的数独](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No36_isvalid-sudoku.md)
13-
- [No.38 报数](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No38_count-and-say.md)
14-
- [No.48 旋转图像](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No48_rotate.md)
7+
- [No.14 最长公共前缀](note/No14_longest-common-prefix.md)
8+
- [No.19 删除链表倒数第 n 个节点](note/No19_remove-nth-from-end.md)
9+
- [No.20 有效的括号](note/No20_is-valid.md)
10+
- [No.21 合并两个有序链表](note/No21_merge-two-lists.md)
11+
- [No.26 从排序数组中删除重复项](note/No26_remove-duplicates.md)
12+
- [No.36 有效的数独](note/No36_isvalid-sudoku.md)
13+
- [No.38 报数](note/No38_count-and-say.md)
14+
- [No.48 旋转图像](note/No48_rotate.md)
1515
- [No.53 最大子序和](note/No53_max-sub-array.md)
16-
- [No.66 加一](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No66_plus-one.md)
17-
- [No.70 爬楼梯](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No70_climb-stairs.md)
18-
- [No.88 合并两个有序数组](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No88_merge.md)
19-
- [No.98 验证二叉搜索树](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No98_is-valid-BST.md)
20-
- [No.104 二叉树的最大深度](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No104_max-depth.md)
21-
- [No.121 买卖股票的最佳时机](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No121_max-profit.md)
22-
- [No.122 买卖股票的最佳时机 II](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No122_max-profit.md)
23-
- [No.125 验证回文串](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No125_is-palindrome.md)
24-
- [No.136 只出现一次的数字](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No136_single-number.md)
25-
- [No.141 环形链表](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No141_has_cycle.md)
26-
- [No.155 最小栈](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No155_min-stack.md)
27-
- [No.160 相交链表](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No160_get-intersection-node.md)
28-
- [No.169 多数元素](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No169_majority-element.md)
29-
- [No.189 旋转数组](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No189_rotate-arr.md)
16+
- [No.66 加一](note/No66_plus-one.md)
17+
- [No.70 爬楼梯](note/No70_climb-stairs.md)
18+
- [No.88 合并两个有序数组](note/No88_merge.md)
19+
- [No.98 验证二叉搜索树](note/No98_is-valid-BST.md)
20+
- [No.104 二叉树的最大深度](note/No104_max-depth.md)
21+
- [No.121 买卖股票的最佳时机](note/No121_max-profit.md)
22+
- [No.122 买卖股票的最佳时机 II](note/No122_max-profit.md)
23+
- [No.125 验证回文串](note/No125_is-palindrome.md)
24+
- [No.136 只出现一次的数字](note/No136_single-number.md)
25+
- [No.141 环形链表](note/No141_has_cycle.md)
26+
- [No.155 最小栈](note/No155_min-stack.md)
27+
- [No.160 相交链表](note/No160_get-intersection-node.md)
28+
- [No.169 多数元素](note/No169_majority-element.md)
29+
- [No.189 旋转数组](note/No189_rotate-arr.md)
3030
- [No.191 位1的个数](note/No191_hamming-weight.md)
31-
- [No.198 打家劫舍](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No198_rob.md)
32-
- [No.204 计数质数](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No204_count-primes.md)
33-
- [No.206 反转链表](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No206_reverse-list.md)
34-
- [No.234 回文链表](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No234_is-palindrome.md)
35-
- [No.237 删除链表节点](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No237_delete-node.md)
36-
- [No.242 有效的字母异位词](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No242_is-anagram.md)
37-
- [No.283 移动零](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No283_move-zeroes.md)
38-
- [No.326 3的幂](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No326_is-power-of-three.md)
39-
- [No.350 两个数组的交集 II](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No350_intersect.md)
31+
- [No.198 打家劫舍](note/No198_rob.md)
32+
- [No.204 计数质数](note/No204_count-primes.md)
33+
- [No.206 反转链表](note/No206_reverse-list.md)
34+
- [No.234 回文链表](note/No234_is-palindrome.md)
35+
- [No.237 删除链表节点](note/No237_delete-node.md)
36+
- [No.242 有效的字母异位词](note/No242_is-anagram.md)
37+
- [No.268 缺失的数字](note/No268_missing-number.md)
38+
- [No.283 移动零](note/No283_move-zeroes.md)
39+
- [No.326 3的幂](note/No326_is-power-of-three.md)
40+
- [No.350 两个数组的交集 II](note/No350_intersect.md)
4041
- [No.384 打乱数组](note/No384_shuffle.md)
41-
- [No.387 字符串中的第一个唯一字符](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No387_first-uniq-char.md)
42-
- [No.412 Fizz Buzz](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No412_fizz-buzz.md)
43-
- [No.448 找到所有数组中消失的数字](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No448_find-disappeared-numbers.md)
44-
- [No.492 构造矩形](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No492_construct-rectangle.md)
45-
- [No.581 最短无序连续子数组](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No581_find-unsorted-subarray.md)
42+
- [No.387 字符串中的第一个唯一字符](note/No387_first-uniq-char.md)
43+
- [No.412 Fizz Buzz](note/No412_fizz-buzz.md)
44+
- [No.448 找到所有数组中消失的数字](note/No448_find-disappeared-numbers.md)
45+
- [No.492 构造矩形](note/No492_construct-rectangle.md)
46+
- [No.581 最短无序连续子数组](note/No581_find-unsorted-subarray.md)
4647

4748
## 分类
4849

@@ -51,63 +52,63 @@
5152
#### 数组初级
5253

5354

54-
- [No.1 两数之和](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No1_two-sum.md)
55-
- [No.26 从排序数组中删除重复项](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No26_remove-duplicates.md)
56-
- [No.36 有效的数独](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No36_isvalid-sudoku.md)
57-
- [No.48 旋转图像](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No48_rotate.md)
58-
- [No.66 加一](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No66_plus-one.md)
59-
- [No.122 买卖股票的最佳时机 II](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No122_max-profit.md)
60-
- [No.136 只出现一次的数字](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No136_single-number.md)
61-
- [No.189 旋转数组](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No189_rotate-arr.md)
62-
- [No.283 移动零](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No283_move-zeroes.md)
63-
- [No.350 两个数组的交集 II](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No350_intersect.md)
64-
- [No.448 找到所有数组中消失的数字](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No448_find-disappeared-numbers.md)
65-
- [No.581 最短无序连续子数组](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No581_find-unsorted-subarray.md)
55+
- [No.1 两数之和](note/No1_two-sum.md)
56+
- [No.26 从排序数组中删除重复项](note/No26_remove-duplicates.md)
57+
- [No.36 有效的数独](note/No36_isvalid-sudoku.md)
58+
- [No.48 旋转图像](note/No48_rotate.md)
59+
- [No.66 加一](note/No66_plus-one.md)
60+
- [No.122 买卖股票的最佳时机 II](note/No122_max-profit.md)
61+
- [No.136 只出现一次的数字](note/No136_single-number.md)
62+
- [No.189 旋转数组](note/No189_rotate-arr.md)
63+
- [No.283 移动零](note/No283_move-zeroes.md)
64+
- [No.350 两个数组的交集 II](note/No350_intersect.md)
65+
- [No.448 找到所有数组中消失的数字](note/No448_find-disappeared-numbers.md)
66+
- [No.581 最短无序连续子数组](note/No581_find-unsorted-subarray.md)
6667

6768
### 字符串
6869

6970
#### 字符串初级
7071

71-
- [No.7 整数反转](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No7_reverse.md)
72-
- [No.8 字符串转换整数 (atoi)](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No8_my-atoi.md)
73-
- [No.14 最长公共前缀](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No14_longest-common-prefix.md)
74-
- [No.28 实现 strStr()](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No28_str-str.md)
75-
- [No.38 报数](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No38_count-and-say.md)
76-
- [No.125 验证回文串](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No125_is-palindrome.md)
77-
- [No.242 有效的字母异位词](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No242_is-anagram.md)
78-
- [No.387 字符串中的第一个唯一字符](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No387_first-uniq-char.md)
72+
- [No.7 整数反转](note/No7_reverse.md)
73+
- [No.8 字符串转换整数 (atoi)](note/No8_my-atoi.md)
74+
- [No.14 最长公共前缀](note/No14_longest-common-prefix.md)
75+
- [No.28 实现 strStr()](note/No28_str-str.md)
76+
- [No.38 报数](note/No38_count-and-say.md)
77+
- [No.125 验证回文串](note/No125_is-palindrome.md)
78+
- [No.242 有效的字母异位词](note/No242_is-anagram.md)
79+
- [No.387 字符串中的第一个唯一字符](note/No387_first-uniq-char.md)
7980

8081
### 链表
8182

8283
#### 链表初级
8384

84-
- [No.19 删除链表倒数第 n 个节点](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No19_remove-nth-from-end.md)
85-
- [No.21 合并两个有序链表](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No21_merge-two-lists.md)
86-
- [No.141 环形链表](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No141_has_cycle.md)
87-
- [No.206 反转链表](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No206_reverse-list.md)
88-
- [No.234 回文链表](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No234_is-palindrome.md)
89-
- [No.237 删除链表节点](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No237_delete-node.md)
85+
- [No.19 删除链表倒数第 n 个节点](note/No19_remove-nth-from-end.md)
86+
- [No.21 合并两个有序链表](note/No21_merge-two-lists.md)
87+
- [No.141 环形链表](note/No141_has_cycle.md)
88+
- [No.206 反转链表](note/No206_reverse-list.md)
89+
- [No.234 回文链表](note/No234_is-palindrome.md)
90+
- [No.237 删除链表节点](note/No237_delete-node.md)
9091

9192

9293
### 数学
9394

9495
#### 数学初级
9596

9697
- [No.13 罗马数字转整数](note/No13_roman-to-int.md)
97-
- [No.326 3的幂](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No326_is-power-of-three.md)
98-
- [No.204 计数质数](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No204_count-primes.md)
99-
- [No.492 构造矩形](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No492_construct-rectangle.md)
98+
- [No.326 3的幂](note/No326_is-power-of-three.md)
99+
- [No.204 计数质数](note/No204_count-primes.md)
100+
- [No.492 构造矩形](note/No492_construct-rectangle.md)
100101

101102

102103
### 动态规划
103104

104105
- [No.53 最大子序和](note/No53_max-sub-array.md)
105-
- [No.70 爬楼梯](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No70_climb-stairs.md)
106-
- [No.121 买卖股票的最佳时机](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No121_max-profit.md)
107-
- [No.198 打家劫舍](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No198_rob.md)
106+
- [No.70 爬楼梯](note/No70_climb-stairs.md)
107+
- [No.121 买卖股票的最佳时机](note/No121_max-profit.md)
108+
- [No.198 打家劫舍](note/No198_rob.md)
108109

109110

110111
### 排序和搜索
111112

112-
- [No.88 合并两个有序数组](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No88_merge.md)
113-
- [No.278 第一个错误的版本](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No278_is-bad-version.md)
113+
- [No.88 合并两个有序数组](note/No88_merge.md)
114+
- [No.278 第一个错误的版本](note/No278_is-bad-version.md)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# No.268 缺失数字
2+
3+
给定一个包含 0, 1, 2, ..., n 中 n 个数的序列,找出 0 .. n 中没有出现在序列中的那个数。
4+
5+
## 示例
6+
7+
示例 1:
8+
9+
```
10+
输入: [3,0,1]
11+
输出: 2
12+
```
13+
14+
示例 2:
15+
```
16+
输入: [9,6,4,2,3,5,7,0,1]
17+
输出: 8
18+
```
19+
说明:
20+
你的算法应具有线性时间复杂度。你能否仅使用额外常数空间来实现?
21+
22+
23+
24+
## 解题思路
25+
26+
用数学的思维方式,假如没有数字缺失,则total满足等差数列的求和公式。减去原数组的和,则剩下的数为缺失的数字。
27+
28+
代码如下:
29+
30+
```javascript
31+
/**
32+
* @param {number[]} nums
33+
* @return {number}
34+
*/
35+
var missingNumber = function(nums) {
36+
let n = nums.length+1;
37+
// 等差数列求和
38+
let total = (n-1)*n/2;
39+
for (let i = 0; i < n-1; i++) {
40+
total -= nums[i];
41+
}
42+
return total;
43+
};
44+
```

0 commit comments

Comments
 (0)