Skip to content

Commit 808834b

Browse files
committed
init
0 parents  commit 808834b

File tree

5 files changed

+86
-0
lines changed

5 files changed

+86
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/target
2+
/Cargo.lock

Cargo.toml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "leet-rs"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]

src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
mod two_sum;
2+
mod util;

src/two_sum.rs

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
}

src/util.rs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#[cfg(test)]
2+
pub struct TestCase<I, E> {
3+
pub input: I,
4+
pub expected: E,
5+
}
6+
7+
#[cfg(test)]
8+
impl<I, E> TestCase<I, E> {
9+
pub fn new(input: I, expected: E) -> Self {
10+
TestCase { input, expected }
11+
}
12+
13+
pub fn input(&self) -> &I {
14+
&self.input
15+
}
16+
17+
pub fn expected(&self) -> &E {
18+
&self.expected
19+
}
20+
}

0 commit comments

Comments
 (0)