题解 | #两数之和# Rust
两数之和
https://www.nowcoder.com/practice/20ef0972485e41019e39543e8e895b7f
use std::collections::HashMap; struct Solution{ } impl Solution { fn new() -> Self { Solution{} } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param numbers int整型一维数组 * @param target int整型 * @return int整型一维数组 */ pub fn twoSum(&self, numbers: Vec<i32>, target: i32) -> Vec<i32> { // write code here let mut hash_table = HashMap::new(); for (index,val) in numbers.iter().enumerate() { if hash_table.contains_key(val) { return vec![(hash_table[val] +1) as i32, (index +1) as i32]; } hash_table.insert(target - val, index); } return vec![0,0] } }