Skip to content

Commit 136969d

Browse files
committed
src/bin/missing-number.rs
1 parent a7918e9 commit 136969d

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

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)