Skip to content

Commit 539d997

Browse files
committed
01-08-2020
1 parent 76dae6b commit 539d997

File tree

6 files changed

+90
-1
lines changed

6 files changed

+90
-1
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ authors = ["chengsu"]
55
edition = "2018"
66

77
[dependencies]
8-
leetcode_prelude = "0.2"
8+
leetcode_prelude = "0.2"
9+
util = "0.1.2"

src/_0001_two_sum.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
struct Solution;
2+
use std::collections::HashMap;
3+
4+
impl Solution {
5+
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
6+
let mut number_map = HashMap::with_capacity(nums.len());
7+
for (idx, num) in nums.into_iter().enumerate() {
8+
if number_map.contains_key(&num) {
9+
return vec![number_map[&num] as i32, idx as i32];
10+
} else {
11+
number_map.insert(target - num, idx);
12+
}
13+
}
14+
vec![]
15+
}
16+
}
17+
18+
#[test]
19+
fn test() {
20+
assert_eq!(Solution::two_sum(vec![2, 7, 11, 15], 9), vec![0, 1]);
21+
}

src/_0021_merge_two_sorted_lists.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
struct Solution;
2+
use crate::util::*;
3+
4+
impl Solution {
5+
fn merge_two_lists(l1: ListLink, l2: ListLink) -> ListLink {
6+
if l1.is_none() {
7+
return l2;
8+
}
9+
if l2.is_none() {
10+
return l1;
11+
}
12+
let mut p1 = l1.unwrap();
13+
let mut p2 = l2.unwrap();
14+
if p1.val < p2.val {
15+
p1.next = Self::merge_two_lists(p1.next, Some(p2));
16+
Some(p1)
17+
} else {
18+
p2.next = Self::merge_two_lists(Some(p1), p2.next);
19+
Some(p2)
20+
}
21+
}
22+
}
23+
24+
#[test]
25+
fn test() {
26+
let a = ListNode::list(vec![1, 2, 4]);
27+
let b = ListNode::list(vec![1, 3, 4]);
28+
let c = ListNode::list(vec![1, 1, 2, 3, 4, 4]);
29+
assert_eq!(Solution::merge_two_lists(a, b), c);
30+
}

src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,16 @@
55
#![allow(clippy::needless_range_loop)]
66
#![allow(clippy::float_cmp)]
77
#![allow(clippy::cast_lossless)]
8+
9+
//
10+
mod util;
11+
//
12+
mod _0001_two_sum;
813
//
914
mod _0020_valid_parentheses;
1015
//
16+
mod _0021_merge_two_sorted_lists;
17+
//
1118
mod _0202_happy_number;
1219
//
1320
mod _1281_subtract_the_product_and_sum_of_digits_of_an_integer;

src/util.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#[derive(PartialEq, Eq, Clone, Debug)]
2+
pub struct ListNode {
3+
pub val: i32,
4+
pub next: ListLink,
5+
}
6+
7+
pub type ListLink = Option<Box<ListNode>>;
8+
9+
impl ListNode {
10+
pub fn list(v: Vec<i32>) -> ListLink {
11+
let mut next: ListLink = None;
12+
for &val in v.iter().rev() {
13+
next = Some(Box::new(ListNode { val, next }))
14+
}
15+
next
16+
}
17+
}
18+
19+
impl ListNode {
20+
pub fn node(val: i32, next: ListLink) -> ListLink {
21+
Some(Box::new(ListNode { val, next }))
22+
}
23+
}

0 commit comments

Comments
 (0)