leet-code/missing-number/sol.rs

8 lines
229 B
Rust
Raw Permalink Normal View History

2022-05-29 19:21:38 +00:00
impl Solution {
pub fn missing_number(nums: Vec<i32>) -> i32 {
let expected_sum = (nums.len() * (nums.len() + 1) / 2) as i32;
nums
.iter()
.fold(expected_sum, |acc, x| acc - *x)
}
}