Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e6527db

Browse files
feat: add solutions to lc problems: No.0268,0645 (doocs#1803)
* No.0268.Missing Number * No.0645.Set Mismatch
1 parent f5180db commit e6527db

File tree

15 files changed

+339
-65
lines changed

15 files changed

+339
-65
lines changed

‎solution/0100-0199/0136.Single Number/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060

6161
我们对该数组所有元素进行异或运算,结果就是那个只出现一次的数字。
6262

63-
时间复杂度 $O(n),ドル空间复杂度 $O(1)$。其中 $n$ 是数组 `nums` 的长度。
63+
时间复杂度 $O(n),ドル空间复杂度 $O(1)$。其中 $n$ 是数组 $nums$ 的长度。
6464

6565
<!-- tabs:start -->
6666

‎solution/0100-0199/0136.Single Number/README_EN.md‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@
3030

3131
## Solutions
3232

33+
**Solution 1: Bitwise Operation**
34+
35+
The XOR operation has the following properties:
36+
37+
- Any number XOR 0 is still the original number, i.e., $x \oplus 0 = x$;
38+
- Any number XOR itself is 0, i.e., $x \oplus x = 0$;
39+
40+
Performing XOR operation on all elements in the array will result in the number that only appears once.
41+
42+
The time complexity is $O(n),ドル where $n$ is the length of the array. The space complexity is $O(1)$.
43+
3344
<!-- tabs:start -->
3445

3546
### **Python3**

‎solution/0200-0299/0268.Missing Number/README.md‎

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,20 @@
6262

6363
**方法一:位运算**
6464

65-
对于数组中的每个元素,都可以与下标进行异或运算,最终的结果就是缺失的数字。
65+
异或运算的性质:
66+
67+
- 任何数和 0ドル$ 做异或运算,结果仍然是原来的数,即 $x \oplus 0 = x$;
68+
- 任何数和其自身做异或运算,结果是 0ドル,ドル即 $x \oplus x = 0$;
69+
70+
因此,我们可以遍历数组,将数字 $[0,..n]$ 与数组中的元素进行异或运算,最后的结果就是缺失的数字。
6671

6772
时间复杂度 $O(n),ドル空间复杂度 $O(1)$。其中 $n$ 为数组长度。
6873

6974
**方法二:数学**
7075

7176
我们也可以用数学求解。求出 $[0,..n]$ 的和,减去数组中所有数的和,就得到了缺失的数字。
7277

73-
时间复杂度 $O(n),ドル空间复杂度 $O(1)$。其中 $n$ 为数组长度
78+
时间复杂度 $O(n),ドル其中 $n$ 为数组长度。空间复杂度 $O(1)$。
7479

7580
<!-- tabs:start -->
7681

@@ -171,6 +176,58 @@ func missingNumber(nums []int) (ans int) {
171176
}
172177
```
173178

179+
### **Rust**
180+
181+
```rust
182+
impl Solution {
183+
pub fn missing_number(nums: Vec<i32>) -> i32 {
184+
let n = nums.len() as i32;
185+
let mut ans = n;
186+
for (i, v) in nums.iter().enumerate() {
187+
ans ^= i as i32 ^ v;
188+
}
189+
ans
190+
}
191+
}
192+
```
193+
194+
```rust
195+
impl Solution {
196+
pub fn missing_number(nums: Vec<i32>) -> i32 {
197+
let n = nums.len() as i32;
198+
let mut ans = n;
199+
for (i, &v) in nums.iter().enumerate() {
200+
ans += i as i32 - v;
201+
}
202+
ans
203+
}
204+
}
205+
```
206+
207+
### **TypeScript**
208+
209+
```ts
210+
function missingNumber(nums: number[]): number {
211+
const n = nums.length;
212+
let ans = n;
213+
for (let i = 0; i < n; ++i) {
214+
ans ^= i ^ nums[i];
215+
}
216+
return ans;
217+
}
218+
```
219+
220+
```ts
221+
function missingNumber(nums: number[]): number {
222+
const n = nums.length;
223+
let ans = n;
224+
for (let i = 0; i < n; ++i) {
225+
ans += i - nums[i];
226+
}
227+
return ans;
228+
}
229+
```
230+
174231
### **JavaScript**
175232

176233
```js

‎solution/0200-0299/0268.Missing Number/README_EN.md‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,23 @@
4646

4747
## Solutions
4848

49+
**Solution 1: Bitwise Operation**
50+
51+
The XOR operation has the following properties:
52+
53+
- Any number XOR 0 is still the original number, i.e., $x \oplus 0 = x$;
54+
- Any number XOR itself is 0, i.e., $x \oplus x = 0$;
55+
56+
Therefore, we can traverse the array, perform XOR operation between each element and the numbers $[0,..n],ドル and the final result will be the missing number.
57+
58+
The time complexity is $O(n),ドル where $n$ is the length of the array. The space complexity is $O(1)$.
59+
60+
**Solution 2: Mathematics**
61+
62+
We can also solve this problem using mathematics. By calculating the sum of $[0,..n],ドル subtracting the sum of all numbers in the array, we can obtain the missing number.
63+
64+
The time complexity is $O(n),ドル where $n$ is the length of the array. The space complexity is $O(1)$.
65+
4966
<!-- tabs:start -->
5067

5168
### **Python3**
@@ -141,6 +158,58 @@ func missingNumber(nums []int) (ans int) {
141158
}
142159
```
143160

161+
### **Rust**
162+
163+
```rust
164+
impl Solution {
165+
pub fn missing_number(nums: Vec<i32>) -> i32 {
166+
let n = nums.len() as i32;
167+
let mut ans = n;
168+
for (i, v) in nums.iter().enumerate() {
169+
ans ^= i as i32 ^ v;
170+
}
171+
ans
172+
}
173+
}
174+
```
175+
176+
```rust
177+
impl Solution {
178+
pub fn missing_number(nums: Vec<i32>) -> i32 {
179+
let n = nums.len() as i32;
180+
let mut ans = n;
181+
for (i, &v) in nums.iter().enumerate() {
182+
ans += i as i32 - v;
183+
}
184+
ans
185+
}
186+
}
187+
```
188+
189+
### **TypeScript**
190+
191+
```ts
192+
function missingNumber(nums: number[]): number {
193+
const n = nums.length;
194+
let ans = n;
195+
for (let i = 0; i < n; ++i) {
196+
ans ^= i ^ nums[i];
197+
}
198+
return ans;
199+
}
200+
```
201+
202+
```ts
203+
function missingNumber(nums: number[]): number {
204+
const n = nums.length;
205+
let ans = n;
206+
for (let i = 0; i < n; ++i) {
207+
ans += i - nums[i];
208+
}
209+
return ans;
210+
}
211+
```
212+
144213
### **JavaScript**
145214

146215
```js
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
impl Solution {
2+
pub fn missing_number(nums: Vec<i32>) -> i32 {
3+
let n = nums.len() as i32;
4+
let mut ans = n;
5+
for (i, v) in nums.iter().enumerate() {
6+
ans ^= i as i32 ^ v;
7+
}
8+
ans
9+
}
10+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function missingNumber(nums: number[]): number {
2+
const n = nums.length;
3+
let ans = n;
4+
for (let i = 0; i < n; ++i) {
5+
ans ^= i ^ nums[i];
6+
}
7+
return ans;
8+
}

‎solution/0200-0299/0287.Find the Duplicate Number/README_EN.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@
4545

4646
## Solutions
4747

48+
**Solution 1: Binary Search**
49+
50+
We can observe that if the number of elements in $[1,..x]$ is greater than $x,ドル then the duplicate number must be in $[1,..x],ドル otherwise the duplicate number must be in $[x+1,..n]$.
51+
52+
Therefore, we can use binary search to find $x,ドル and check whether the number of elements in $[1,..x]$ is greater than $x$ at each iteration. This way, we can determine which interval the duplicate number is in, and narrow down the search range until we find the duplicate number.
53+
54+
The time complexity is $O(n \times \log n),ドル where $n$ is the length of the array $nums$. The space complexity is $O(1)$.
55+
4856
<!-- tabs:start -->
4957

5058
### **Python3**

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /