From 136969d6a0db21bfe58559424e0a3b1476844d79 Mon Sep 17 00:00:00 2001 From: bestgopher <84328409@qq.com> Date: Thu, 15 Jul 2021 21:13:38 +0800 Subject: [PATCH 1/2] src/bin/missing-number.rs --- src/bin/missing-number.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/bin/missing-number.rs 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 + }) + } +} From 199a5e3df371dd3be88f57eb640432967394096e Mon Sep 17 00:00:00 2001 From: bestgopher <84328409@qq.com> Date: Thu, 15 Jul 2021 21:13:38 +0800 Subject: [PATCH 2/2] README.md --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) 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/)