Skip to content

Commit 85e68b6

Browse files
committed
add 268
1 parent e9bd5d4 commit 85e68b6

File tree

3 files changed

+126
-6
lines changed

3 files changed

+126
-6
lines changed

README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
My personal leetcode answers<br/>
66
This is a **continually updated** open source project<br/>
77
<br/>
8-
Total sovled: **363**<br/>
9-
Auto updated at: **2024-07-31 12:41:29**<br/>
8+
Total sovled: **364**<br/>
9+
Auto updated at: **2024-07-31 12:57:56**<br/>
1010

1111
## 软件/Softwares
1212
- [Anki](https://apps.ankiweb.net/)
@@ -60,7 +60,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
6060
- [Segment Tree](#segment-tree)
6161

6262
## Math
63-
| Problem(24) | Solution | Time | Space | Note | Ref |
63+
| Problem(25) | Solution | Time | Space | Note | Ref |
6464
| ----- | ----- | ----- | ----- | ----- | ----- |
6565
| [Leetcode-67. Add Binary](https://leetcode.com/problems/add-binary/description/) | [c++](./leetcode/67.add-binary.cpp), [python](./leetcode/67.add-binary.py) | O\(M\+N\) | O\(1\) | \- | - |
6666
| [Leetcode-69. Sqrtx](https://leetcode.com/problems/sqrtx/description/) | [c++](./leetcode/69.sqrtx.cpp), [python](./leetcode/69.sqrtx.py) | N\(logN\) | O\(1\) | \- | - |
@@ -71,6 +71,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
7171
| [Leetcode-204. Count Primes](https://leetcode.com/problems/count-primes/description/) | [c++](./leetcode/204.count-primes.cpp), [python](./leetcode/204.count-primes.py) | O\(NlogN\) | O\(N\) | \- | - |
7272
| [Leetcode-241. Different Ways To Add Parentheses](https://leetcode.com/problems/different-ways-to-add-parentheses/description/) | [c++](./leetcode/241.different-ways-to-add-parentheses.cpp), [python](./leetcode/241.different-ways-to-add-parentheses.py) | \- | \- | \- | - |
7373
| [Leetcode-263. Ugly Number](https://leetcode.com/problems/ugly-number/description/) | [c++](./leetcode/263.ugly-number.cpp), [python](./leetcode/263.ugly-number.py) | O\(k\) | O\(1\) | \- | - |
74+
| [Leetcode-268. Missing Number](https://leetcode.com/problems/missing-number/description/) | [python](./leetcode/268.missing-number.py), [c++](./leetcode/268.missing-number.cpp) | O\(N\) | O\(1\) | \- | - |
7475
| [Leetcode-279. Perfect Squares](https://leetcode.com/problems/perfect-squares/description/) | [c++](./leetcode/279.perfect-squares.cpp), [python](./leetcode/279.perfect-squares.py) | O\(n^\{3/2\}\) | O\(N\) | \- | - |
7576
| [Leetcode-326. Power Of Three](https://leetcode.com/problems/power-of-three/description/) | [python](./leetcode/326.power-of-three.py), [c++](./leetcode/326.power-of-three.cpp) | O\(1\) | O\(1\) | \- | - |
7677
| [Leetcode-342. Power Of Four](https://leetcode.com/problems/power-of-four/description/) | [python](./leetcode/342.power-of-four.py), [c++](./leetcode/342.power-of-four.cpp) | O\(1\) | O\(1\) | \- | - |
@@ -88,11 +89,12 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
8889
| [Leetcode-932. Beautiful Array](https://leetcode.com/problems/beautiful-array/description/) | [python](./leetcode/932.beautiful-array.py), [c++](./leetcode/932.beautiful-array.cpp) | O\(N\) | O\(N\) | dp | - |
8990

9091
## Bit Manipulation
91-
| Problem(7) | Solution | Time | Space | Note | Ref |
92+
| Problem(8) | Solution | Time | Space | Note | Ref |
9293
| ----- | ----- | ----- | ----- | ----- | ----- |
9394
| [Leetcode-67. Add Binary](https://leetcode.com/problems/add-binary/description/) | [c++](./leetcode/67.add-binary.cpp), [python](./leetcode/67.add-binary.py) | O\(M\+N\) | O\(1\) | \- | - |
9495
| [Leetcode-136. Single Number](https://leetcode.com/problems/single-number/description/) | [c++](./leetcode/136.single-number.cpp), [python](./leetcode/136.single-number.py) | O\(N\) | O\(1\) | \- | - |
9596
| [Leetcode-190. Reverse Bits](https://leetcode.com/problems/reverse-bits/description/) | [python](./leetcode/190.reverse-bits.py), [c++](./leetcode/190.reverse-bits.cpp) | O\(1\) | O\(1\) | \- | - |
97+
| [Leetcode-268. Missing Number](https://leetcode.com/problems/missing-number/description/) | [python](./leetcode/268.missing-number.py), [c++](./leetcode/268.missing-number.cpp) | O\(N\) | O\(1\) | \- | - |
9698
| [Leetcode-318. Maximum Product Of Word Lengths](https://leetcode.com/problems/maximum-product-of-word-lengths/description/) | [c++](./leetcode/318.maximum-product-of-word-lengths.cpp), [python](./leetcode/318.maximum-product-of-word-lengths.py) | O\(N^2\) | O\(N\) | \- | - |
9799
| [Leetcode-338. Counting Bits](https://leetcode.com/problems/counting-bits/description/) | [python](./leetcode/338.counting-bits.py), [c++](./leetcode/338.counting-bits.cpp) | O\(N\) | O\(1\) | \- | - |
98100
| [Leetcode-342. Power Of Four](https://leetcode.com/problems/power-of-four/description/) | [python](./leetcode/342.power-of-four.py), [c++](./leetcode/342.power-of-four.cpp) | O\(1\) | O\(1\) | \- | - |
@@ -112,7 +114,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
112114
| [Leetcode-297. Serialize And Deserialize Binary Tree](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/description/) | [c++](./leetcode/297.serialize-and-deserialize-binary-tree.cpp), [python](./leetcode/297.serialize-and-deserialize-binary-tree.py) | O\(N\) | O\(N\) | Serialization | - |
113115

114116
## Binary Search
115-
| Problem(34) | Solution | Time | Space | Note | Ref |
117+
| Problem(35) | Solution | Time | Space | Note | Ref |
116118
| ----- | ----- | ----- | ----- | ----- | ----- |
117119
| [Leetcode-4. Median Of Two Sorted Arrays](https://leetcode.com/problems/median-of-two-sorted-arrays/description/) | [python](./leetcode/4.median-of-two-sorted-arrays.py), [c++](./leetcode/4.median-of-two-sorted-arrays.cpp) | O\(log\(min\(M, N\)\)\) | O\(1\) | \- | - |
118120
| [Leetcode-33. Search In Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/description/) | [python](./leetcode/33.search-in-rotated-sorted-array.py), [c++](./leetcode/33.search-in-rotated-sorted-array.cpp) | O\(logN\) | O\(1\) | Rotated; | - |
@@ -128,6 +130,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
128130
| [Leetcode-167. Two Sum Ii Input Array Is Sorted](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/) | [c++](./leetcode/167.two-sum-ii-input-array-is-sorted.cpp), [python](./leetcode/167.two-sum-ii-input-array-is-sorted.py) | O\(N\) | O\(1\) | \- | - |
129131
| [Leetcode-173. Binary Search Tree Iterator](https://leetcode.com/problems/binary-search-tree-iterator/description/) | [python](./leetcode/173.binary-search-tree-iterator.py), [c++](./leetcode/173.binary-search-tree-iterator.cpp) | O\(1\) | O\(Height\) | InOrder | - |
130132
| [Leetcode-240. Search A 2D Matrix II](https://leetcode.com/problems/search-a-2d-matrix-ii/description/) | [python](./leetcode/240.search-a-2d-matrix-ii.py), [c++](./leetcode/240.search-a-2d-matrix-ii.cpp) | O\(m \* logN\) | O\(1\) | Matrix | - |
133+
| [Leetcode-268. Missing Number](https://leetcode.com/problems/missing-number/description/) | [python](./leetcode/268.missing-number.py), [c++](./leetcode/268.missing-number.cpp) | O\(N\) | O\(1\) | \- | - |
131134
| [Leetcode-278. First Bad Version](https://leetcode.com/problems/first-bad-version/description/) | [python](./leetcode/278.first-bad-version.py), [c++](./leetcode/278.first-bad-version.cpp) | O\(logN\) | O\(1\) | Range; | [Video](https://youtu.be/xNzBpfqzYSg) |
132135
| [Leetcode-300. Longest Increasing Subsequence](https://leetcode.com/problems/longest-increasing-subsequence/description/) | [python](./leetcode/300.longest-increasing-subsequence.py), [c++](./leetcode/300.longest-increasing-subsequence.cpp) | O\(N\*logN\) | O\(N\) | LIS \| std::lower\_bound | - |
133136
| [Leetcode-374. Guess Number Higher Or Lower](https://leetcode.com/problems/guess-number-higher-or-lower/description/) | [python](./leetcode/374.guess-number-higher-or-lower.py), [c++](./leetcode/374.guess-number-higher-or-lower.cpp) | O\(logN\) | O\(1\) | Standard | - |
@@ -192,12 +195,13 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
192195
| [Leetcode-1249. Minimum Remove To Make Valid Parentheses](https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/description/) | [c++](./leetcode/1249.minimum-remove-to-make-valid-parentheses.cpp), [python](./leetcode/1249.minimum-remove-to-make-valid-parentheses.py) | O\(N\) | O\(N\) | \- | - |
193196

194197
## Sorting
195-
| Problem(15) | Solution | Time | Space | Note | Ref |
198+
| Problem(16) | Solution | Time | Space | Note | Ref |
196199
| ----- | ----- | ----- | ----- | ----- | ----- |
197200
| [Leetcode-75. Sort Colors](https://leetcode.com/problems/sort-colors/description/) | [python](./leetcode/75.sort-colors.py), [c++](./leetcode/75.sort-colors.cpp) | O\(N\) | O\(1\) | \- | - |
198201
| [Leetcode-88. Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array/description/) | [python](./leetcode/88.merge-sorted-array.py), [c++](./leetcode/88.merge-sorted-array.cpp) | O\(M \+ N\) | O\(1\) | \- | - |
199202
| [Leetcode-169. Majority Element](https://leetcode.com/problems/majority-element/description/) | [python](./leetcode/169.majority-element.py), [c++](./leetcode/169.majority-element.cpp) | O\(N\) | O\(1\) | \- | - |
200203
| [Leetcode-215. Kth Largest Element In An Array](https://leetcode.com/problems/kth-largest-element-in-an-array/description/) | [python](./leetcode/215.kth-largest-element-in-an-array.py), [c++](./leetcode/215.kth-largest-element-in-an-array.cpp) | O\(N\) ~ O\(N^2\) | O\(1\) | QuickSelection | - |
204+
| [Leetcode-268. Missing Number](https://leetcode.com/problems/missing-number/description/) | [python](./leetcode/268.missing-number.py), [c++](./leetcode/268.missing-number.cpp) | O\(N\) | O\(1\) | \- | - |
201205
| [Leetcode-347. Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/description/) | [python](./leetcode/347.top-k-frequent-elements.py), [c++](./leetcode/347.top-k-frequent-elements.cpp) | O\(N\), O\(C\*logK\) | O\(N\) | \- | - |
202206
| [Leetcode-406. Queue Reconstruction By Height](https://leetcode.com/problems/queue-reconstruction-by-height/description/) | [python](./leetcode/406.queue-reconstruction-by-height.py), [c++](./leetcode/406.queue-reconstruction-by-height.cpp) | O\(N \* logN\) | O\(1\) | \- | - |
203207
| [Leetcode-435. Non Overlapping Intervals](https://leetcode.com/problems/non-overlapping-intervals/description/) | [c++](./leetcode/435.non-overlapping-intervals.cpp), [python](./leetcode/435.non-overlapping-intervals.py) | O\(N \* logN\) | O\(1\) | \- | - |

leetcode/268.missing-number.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Tag: Array, Hash Table, Math, Binary Search, Bit Manipulation, Sorting
2+
// Time: O(N)
3+
// Space: O(1)
4+
// Ref: -
5+
// Note: -
6+
7+
// Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array.
8+
//  
9+
// Example 1:
10+
//
11+
// Input: nums = [3,0,1]
12+
// Output: 2
13+
// Explanation: n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums.
14+
//
15+
// Example 2:
16+
//
17+
// Input: nums = [0,1]
18+
// Output: 2
19+
// Explanation: n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums.
20+
//
21+
// Example 3:
22+
//
23+
// Input: nums = [9,6,4,2,3,5,7,0,1]
24+
// Output: 8
25+
// Explanation: n = 9 since there are 9 numbers, so all numbers are in the range [0,9]. 8 is the missing number in the range since it does not appear in nums.
26+
//
27+
//  
28+
// Constraints:
29+
//
30+
// n == nums.length
31+
// 1 <= n <= 104
32+
// 0 <= nums[i] <= n
33+
// All the numbers of nums are unique.
34+
//
35+
//  
36+
// Follow up: Could you implement a solution using only O(1) extra space complexity and O(n) runtime complexity?
37+
//
38+
39+
class Solution {
40+
public:
41+
int missingNumber(vector<int>& nums) {
42+
int n = nums.size();
43+
int res = 0;
44+
for (int i = 0; i < n; i++) {
45+
res = res ^ (i + 1) ^ nums[i];
46+
}
47+
48+
return res;
49+
}
50+
};
51+
52+
class Solution {
53+
public:
54+
int missingNumber(vector<int>& nums) {
55+
int n = nums.size();
56+
int res = (n + 1) * n / 2;
57+
for (int i = 0; i < n; i++) {
58+
res -= nums[i];
59+
}
60+
61+
return res;
62+
}
63+
};

leetcode/268.missing-number.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Tag: Array, Hash Table, Math, Binary Search, Bit Manipulation, Sorting
2+
# Time: O(N)
3+
# Space: O(1)
4+
# Ref: -
5+
# Note: -
6+
7+
# Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array.
8+
#  
9+
# Example 1:
10+
#
11+
# Input: nums = [3,0,1]
12+
# Output: 2
13+
# Explanation: n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums.
14+
#
15+
# Example 2:
16+
#
17+
# Input: nums = [0,1]
18+
# Output: 2
19+
# Explanation: n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums.
20+
#
21+
# Example 3:
22+
#
23+
# Input: nums = [9,6,4,2,3,5,7,0,1]
24+
# Output: 8
25+
# Explanation: n = 9 since there are 9 numbers, so all numbers are in the range [0,9]. 8 is the missing number in the range since it does not appear in nums.
26+
#
27+
#  
28+
# Constraints:
29+
#
30+
# n == nums.length
31+
# 1 <= n <= 104
32+
# 0 <= nums[i] <= n
33+
# All the numbers of nums are unique.
34+
#
35+
#  
36+
# Follow up: Could you implement a solution using only O(1) extra space complexity and O(n) runtime complexity?
37+
#
38+
39+
class Solution:
40+
def missingNumber(self, nums: List[int]) -> int:
41+
n = len(nums)
42+
res = 0
43+
for i in range(n):
44+
res = res ^ nums[i] ^ (i + 1)
45+
return res
46+
47+
class Solution:
48+
def missingNumber(self, nums: List[int]) -> int:
49+
n = len(nums)
50+
res = (n + 1) * n // 2
51+
for i in range(n):
52+
res -= nums[i]
53+
return res

0 commit comments

Comments
 (0)