典中典,没啥好说的,主要练习一下Rust的二分查找API。
impl Solution {
pub fn two_sum(numbers: Vec<i32>, target: i32) -> Vec<i32> {
let n = numbers.len();
for (i, x) in numbers.iter().enumerate() {
let y = numbers.binary_search(&(target - x));
if let Ok(mut j) = y {
if j == i { j += 1; }
if j < n && numbers[j] == numbers[i] {
return vec![i as i32 + 1, j as i32 + 1];
}
}
}
vec![0, 1]
}
}
标签:i32,II,let,numbers,2023.7,vec,两数,Vec
From: https://www.cnblogs.com/st0rmKR/p/17537022.html