1. Two Sum
use std::collections::HashMap;
impl Solution{
pub fn two_sum(nums: Vec<i32>, target:i32) -> Vec<i32>{
let mut map = HashMap::new();
for(i,v) in nums.into_iter().enumerate(){
if let Some(&x) = map.get(&(target-v)){
return vec![x, i as i32]
}
map.insert(v, i as i32);
}
vec![]
}
}
2. Add Two Numbers
impl Solution {
pub fn add_two_numbers(l1: Option<Box<ListNode>>, l2: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
add_two_numbers(l1,l2,0)
}
}
pub fn add_two_numbers(l1 : Option<Box<ListNode>>, l2 : Option<Box<ListNode>>, mut carry : i32) -> Option<Box<ListNode>>{
if l1.is_none() && l2.is_none() && carry == 0{
return None;
}else{
Some(Box::new(ListNode{
next : add_two_numbers(
l1.and_then(|x| {carry += x.val; x.next}),
l2.and_then(|x| {carry += x.val; x.next}),
carry / 10
),
val : carry % 10
}))
}
}