summaryrefslogtreecommitdiff
path: root/two-sum/src/main.rs
blob: 8b18400dfdca9f67b18450c597dfa0def3cf5ead (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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];
}