1
Fork 0
leetcode/source/missing_number/mod.rs

12 lines
455 B
Rust

pub fn missing_number(numbers: Vec<i32>) -> i32 {
// The range of numbers can be described as the total sum of all possible
// numbers in that range.
let total_range = (0..=numbers.len() as i32).sum::<i32>();
// And since only one number is missing from that range, the difference
// between the range's sum and the numbers' sum will be that missing number.
let sum_of_numbers = numbers.iter().sum::<i32>();
total_range - sum_of_numbers
}