Leetcode

[1] Two Sum

/**
 * [1] Two Sum
 *
 * Given an array of integers, return indices of the two numbers such that they
 * add up to a specific target.
 *
 * You may assume that each input would have exactly one solution, and you may
 * not use the same element twice.
 *
 * Example:
 *
 *
 * Given nums = [2, 7, 11, 15], target = 9,
 *
 * Because nums[0] + nums[1] = 2 + 7 = 9,
 * return [0, 1].
 *
 */
use std::collections::HashMap;
pub struct Solution {}

impl Solution {
    pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
        let mut map = HashMap::with_capacity(nums.len());
        for (index, num) in nums.iter().enumerate() {
            println!("index:{} num:{}", index, num);
            match map.get(&(target - num)) {
                None => {
                    map.insert(num, index);
                }
                Some(sub_index) => return vec![*sub_index as i32, index as i32],
            }
        }
        vec![]
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_1() {
        assert_eq!(vec![0, 1], Solution::two_sum(vec![2, 7, 11, 15], 9));
    }
    #[test]
    fn test_2() {
        assert_eq!(vec![1, 3], Solution::two_sum(vec![2, 5, 8, 9, 13], 14));
    }
    #[test]
    fn test_3() {
        assert_eq!(vec![0, 2], Solution::two_sum(vec![3, 2, 4], 6));
    }
}
cargo test

Finished dev [unoptimized + debuginfo] target(s) in 0.01s
Running target/debug/deps/Demo-48d6ac374a2c21cb

running 3 tests
test tests::test_1 ... ok
test tests::test_2 ... ok
test tests::test_3 ... FAILED

failures:

---- tests::test_3 stdout ----
index:0 num:3
index:1 num:2
index:2 num:4
thread 'tests::test_3' panicked at 'assertion failed: `(left == right)`
  left: `[0, 2]`,
 right: `[1, 2]`', src/main.rs:51:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace.

[2] Search Insert Position

/**
 * [2] Search Insert Position
 * Given a sorted array and a target value, return the index if the target is found.
 * If not, return the index where it would be if it were inserted in order.
 * You may assume no duplicates in the array.
 *
 * Here are few examples.
 * [1,3,5,6], 5 → 2
 * [1,3,5,6], 2 → 1
 * [1,3,5,6], 7 → 4
 * [1,3,5,6], 0 → 0
 *
 */
pub struct Solution {}

impl Solution {
    pub fn two_sum(nums: Vec<i32>, target: i32) -> i32 {
        for (index, num) in nums.iter().enumerate() {
            if *num >= target {
                return index as i32;
            }
        }
        nums.len() as i32
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_1() {
        assert_eq!(2, Solution::two_sum(vec![1, 3, 5, 6], 5));
    }
    #[test]
    fn test_2() {
        assert_eq!(1, Solution::two_sum(vec![1, 3, 5, 6], 2));
    }
    #[test]
    fn test_3() {
        assert_eq!(4, Solution::two_sum(vec![1, 3, 5, 6], 7));
    }
    #[test]
    fn test_4() {
        assert_eq!(0, Solution::two_sum(vec![1, 3, 5, 6], 0));
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章