diff --git a/README.md b/README.md index d4d88beb..4a554598 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # leetcode -当前已刷:237 +当前已刷:238 ### 题目 - 1:两数之和 @@ -438,6 +438,9 @@ - 263:丑数 - [src](https://github.com/rustors/leetcode/blob/main/src/bin/ugly-number.rs) - [leetcode](https://leetcode-cn.com/problems/ugly-number/) +- 268:丢失的数字 + - [src](https://github.com/rustors/leetcode/blob/main/src/bin/missing-number.rs) + - [leetcode](https://leetcode-cn.com/problems/missing-number/) - 290:单词规律 - [src](https://github.com/rustors/leetcode/blob/main/src/bin/word-pattern.rs) - [leetcode](https://leetcode-cn.com/problems/word-pattern/) diff --git a/src/bin/missing-number.rs b/src/bin/missing-number.rs new file mode 100644 index 00000000..2fd9b662 --- /dev/null +++ b/src/bin/missing-number.rs @@ -0,0 +1,19 @@ +fn main() { + println!("{}", Solution::missing_number1(vec![3, 0, 1])); + println!("{}", Solution::missing_number1(vec![9, 6, 4, 2, 3, 5, 7, 0, 1])); +} + +struct Solution; + +impl Solution { + pub fn missing_number(nums: Vec) -> i32 { + (0..=nums.len() as i32).sum::() - nums.into_iter().sum::() + } + + pub fn missing_number1(nums: Vec) -> i32 { + let l = nums.len() as i32; + nums.into_iter().enumerate().fold(l, |l, (x, y)| { + x as i32 ^ y ^ l + }) + } +}