Skip to content

Commit 0731e61

Browse files
authored
Merge pull request #2 from bestgopher/feature/leetcode-new
Feature/leetcode new
2 parents a7918e9 + 199a5e3 commit 0731e61

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# leetcode
22

3-
当前已刷:237
3+
当前已刷:238
44

55
### 题目
66
- 1:两数之和
@@ -438,6 +438,9 @@
438438
- 263:丑数
439439
- [src](https://github.com/rustors/leetcode/blob/main/src/bin/ugly-number.rs)
440440
- [leetcode](https://leetcode-cn.com/problems/ugly-number/)
441+
- 268:丢失的数字
442+
- [src](https://github.com/rustors/leetcode/blob/main/src/bin/missing-number.rs)
443+
- [leetcode](https://leetcode-cn.com/problems/missing-number/)
441444
- 290:单词规律
442445
- [src](https://github.com/rustors/leetcode/blob/main/src/bin/word-pattern.rs)
443446
- [leetcode](https://leetcode-cn.com/problems/word-pattern/)

src/bin/missing-number.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
fn main() {
2+
println!("{}", Solution::missing_number1(vec![3, 0, 1]));
3+
println!("{}", Solution::missing_number1(vec![9, 6, 4, 2, 3, 5, 7, 0, 1]));
4+
}
5+
6+
struct Solution;
7+
8+
impl Solution {
9+
pub fn missing_number(nums: Vec<i32>) -> i32 {
10+
(0..=nums.len() as i32).sum::<i32>() - nums.into_iter().sum::<i32>()
11+
}
12+
13+
pub fn missing_number1(nums: Vec<i32>) -> i32 {
14+
let l = nums.len() as i32;
15+
nums.into_iter().enumerate().fold(l, |l, (x, y)| {
16+
x as i32 ^ y ^ l
17+
})
18+
}
19+
}

0 commit comments

Comments
 (0)