|
| 1 | +#![allow(dead_code)] |
| 2 | +/* |
| 3 | +Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. |
| 4 | +You may assume that each input would have exactly one solution, and you may not use the same element twice. |
| 5 | +You can return the answer in any order. |
| 6 | +
|
| 7 | +Constraints: |
| 8 | +2 <= nums.length <= 10^4 |
| 9 | +-10^9 <= nums[i] <= 10^9 |
| 10 | +-10^9 <= target <= 10^9 |
| 11 | +Only one valid answer exists. |
| 12 | +*/ |
| 13 | + |
| 14 | +pub fn solution_a<N>(nums: N, target: i32) -> (i32, i32) |
| 15 | +where |
| 16 | + N: IntoIterator<Item = i32>, |
| 17 | +{ |
| 18 | + (17, 18) |
| 19 | +} |
| 20 | + |
| 21 | +#[cfg(test)] |
| 22 | +mod tests { |
| 23 | + use super::*; |
| 24 | + use crate::util::TestCase; |
| 25 | + |
| 26 | + const TEST_CASES: [TestCase<([i32; 4], i32), (i32, i32)>; 3] = [ |
| 27 | + TestCase { |
| 28 | + input: ([2i32, 7, 11, 15], 9i32), |
| 29 | + expected: (0, 1), |
| 30 | + }, |
| 31 | + TestCase { |
| 32 | + input: ([3, 2, 4, 0], 6), |
| 33 | + expected: (1, 2), |
| 34 | + }, |
| 35 | + TestCase { |
| 36 | + input: ([3, 3, 0, 0], 6), |
| 37 | + expected: (0, 1), |
| 38 | + }, |
| 39 | + ]; |
| 40 | + |
| 41 | + // let t: static = TestCase::new((vec![2, 7, 11, 15], 9), (0, 1)); |
| 42 | + |
| 43 | + #[test] |
| 44 | + fn test_a() { |
| 45 | + for c in TEST_CASES.iter() { |
| 46 | + let (nums, target) = c.input(); |
| 47 | + let expected = c.expected().clone(); |
| 48 | + |
| 49 | + let res = solution_a(nums.clone(), target.clone()); |
| 50 | + |
| 51 | + assert_eq!(res, expected.clone()); |
| 52 | + } |
| 53 | + } |
| 54 | +} |
0 commit comments