diff options
Diffstat (limited to 'two-sum/src')
| -rw-r--r-- | two-sum/src/main.rs | 28 | 
1 files changed, 28 insertions, 0 deletions
diff --git a/two-sum/src/main.rs b/two-sum/src/main.rs new file mode 100644 index 0000000..8b18400 --- /dev/null +++ b/two-sum/src/main.rs @@ -0,0 +1,28 @@ +fn main() { +    println!("Hello, world!"); +    let tests = [ +        (vec![2,7,11,15], 9, vec![0,1]), +        (vec![3,2,4], 6, vec![1,2]), +        (vec![3,3], 6, vec![0,1]) +    ]; + +    for test in tests { +        println!("{:?} at {} is {:?} should be {:?}", +            test.0, +            test.1, +            two_sum(test.0.clone(), test.1), +            test.2 +                 ); +    } +} + +pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> { +    for i in 0..nums.len()-1 { +        for ii in i+1..nums.len() { +            if nums[i] + nums[ii] == target { +                return vec![i as i32, ii as i32]; +            } +        } +    } +    return vec![0,0]; +}  | 
