Skip to content

Commit 7e3d0d3

Browse files
committed
init docs and dir structure
1 parent bfc9cd8 commit 7e3d0d3

File tree

9 files changed

+98
-0
lines changed

9 files changed

+98
-0
lines changed

.github/workflows/ci.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Cargo Build & Test
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
env:
8+
CARGO_TERM_COLOR: always
9+
10+
jobs:
11+
build_and_test:
12+
name: Rust project - latest
13+
runs-on: ubuntu-latest
14+
strategy:
15+
matrix:
16+
toolchain:
17+
- stable
18+
- beta
19+
- nightly
20+
steps:
21+
- uses: actions/checkout@v4
22+
- run: rustup update ${{ matrix.toolchain }} && rustup default ${{ matrix.toolchain }}
23+
- run: cargo build --verbose
24+
- run: cargo test --verbose
25+

Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
name = "leetcoders"
3+
version = "0.0.1"
4+
edition = "2021"
5+
6+
[dependencies]

src/easy.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/// Given an array of integers `nums` and an integer `target`, return indices of the two numbers such that they add up to `target`.
2+
///
3+
/// You may assume that each input would have **exactly one solution**, and you may not use the same element twice.
4+
///
5+
/// You can return the answer in any order.
6+
///
7+
/// #### Example 1:
8+
/// > **Input:** ```nums = [2,7,11,15], target = 9```
9+
///
10+
/// > **Output:** ```[0,1]```
11+
///
12+
/// > **Explanation:** ```Because nums[0] + nums[1] == 9, we return [0, 1].```
13+
/// #### Example 2:
14+
/// > **Input:** ```nums = [3,2,4], target = 6```
15+
///
16+
/// > **Output:** ```[1,2]```
17+
/// #### Example 3:
18+
/// > **Input:** ```nums = [3,3], target = 6```
19+
///
20+
/// > **Output:** ```[0,1]```
21+
///
22+
/// #### Constraints:
23+
/// - <code>2 <= nums.length <= 10<sup>4</sup></code>
24+
/// - <code>-10<sup>9</sup> <= nums\[i\] <= 10<sup>9</sup></code>
25+
/// - <code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code>
26+
/// - **Only one valid answer exists.**
27+
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
28+
let mut nums: Vec<(usize, i32)> = nums.into_iter().enumerate().collect::<Vec<(usize, i32)>>();
29+
nums.sort_unstable_by_key(|&(_, v)| v);
30+
for (i, (k, v)) in nums.iter().enumerate() {
31+
match nums[i + 1..].binary_search_by_key(&(target - *v), |&(_, b)| b) {
32+
Ok(j) => {
33+
return vec![*k as i32, nums[j + i + 1].0 as i32];
34+
}
35+
Err(_) => {}
36+
}
37+
}
38+
unreachable!();
39+
}

src/hard.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

src/lib.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
pub mod easy;
2+
pub mod hard;
3+
pub mod medium;
4+
5+
pub fn add(left: u64, right: u64) -> u64 {
6+
left + right
7+
}
8+
9+
#[cfg(test)]
10+
mod tests {
11+
use super::*;
12+
13+
#[test]
14+
fn it_works() {
15+
let result = add(2, 2);
16+
assert_eq!(result, 4);
17+
}
18+
}

src/medium.rs

Whitespace-only changes.

tests/easy_tests.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
use leetcoders::easy;
2+
3+
#[test]
4+
fn test_two_sum() {
5+
assert_eq!(easy::two_sum(vec![2, 7, 11, 15], 9), vec![0, 1]);
6+
assert_eq!(easy::two_sum(vec![3, 2, 4], 6), vec![1, 2]);
7+
assert_eq!(easy::two_sum(vec![3, 3], 6), vec![0, 1]);
8+
}

tests/hard_tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

tests/medium_tests.rs

Whitespace-only changes.

0 commit comments

Comments
 (0)