两数之和
use std::collections::HashMap;
impl Solution {
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
let mut vis: HashMap<i32, usize> = Default::default();
for (i, x) in nums.iter().enumerate() {
if vis.contains_key(&x) {
return vec![vis[x] as i32, i as i32];
} else {
vis.insert(target - x, i);
continue;
}
}
vec![]
}
}
impl Solution {
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
let mut rst: Vec<i32> = Vec::new();
for i in 0..nums.len() {
for j in (i+1)..nums.len() {
if (nums[i] + nums[j] == target) {
rst.push(i as i32);
rst.push(j as i32);
break;
}
}
}
rst
}
}