Skip to content

Commit f18f65b

Browse files
committed
number 2: add_two_numbers
1 parent 91c98c3 commit f18f65b

File tree

5 files changed

+118
-3
lines changed

5 files changed

+118
-3
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
name = "leetcode-rust"
33
version = "0.1.0"
44
authors = ["alei <rayingecho@gmail.com>"]
5-
edition = "2019"
5+
edition = "2018"
66

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ Run `cargo test test_{id}` to test the solution for problem #id.
1010

1111
Working in progress, solutions list:
1212

13-
* [1 - two sum](./src/two_sum_1.rs)
13+
* [1 - two sum](src/n1_two_sum.rs)

src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// make cargo happy
2-
pub mod two_sum_1;
2+
pub mod n1_two_sum;
3+
pub mod n2_add_two_numbers;
34

45
fn main() {
56
}

src/two_sum_1.rs renamed to src/n1_two_sum.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
pub struct Solution {}
2323

24+
// submission codes start here
25+
2426
use std::collections::HashMap;
2527
impl Solution {
2628
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
@@ -35,6 +37,8 @@ impl Solution {
3537
}
3638
}
3739

40+
// submission codes end
41+
3842
#[cfg(test)]
3943
mod tests {
4044
use super::*;

src/n2_add_two_numbers.rs

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* [2] Add Two Numbers
3+
*
4+
* https://leetcode.com/problems/add-two-numbers/description/
5+
*
6+
* algorithms
7+
* Medium (30.03%)
8+
* Total Accepted: 705.4K
9+
* Total Submissions: 2.3M
10+
* Testcase Example: '[2,4,3]\n[5,6,4]'
11+
*
12+
* You are given two non-empty linked lists representing two non-negative
13+
* integers. The digits are stored in reverse order and each of their nodes
14+
* contain a single digit. Add the two numbers and return it as a linked list.
15+
*
16+
* You may assume the two numbers do not contain any leading zero, except the
17+
* number 0 itself.
18+
*
19+
* Example:
20+
*
21+
*
22+
* Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
23+
* Output: 7 -> 0 -> 8
24+
* Explanation: 342 + 465 = 807.
25+
*
26+
*/
27+
28+
#[derive(PartialEq, Eq, Debug)]
29+
pub struct ListNode {
30+
pub val: i32,
31+
pub next: Option<Box<ListNode>>
32+
}
33+
34+
impl ListNode {
35+
#[inline]
36+
fn new(val: i32) -> Self {
37+
ListNode {
38+
next: None,
39+
val
40+
}
41+
}
42+
}
43+
44+
// helper function for test
45+
pub fn to_list(vec: Vec<i32>) -> Option<Box<ListNode>> {
46+
let mut current = None;
47+
for &v in vec.iter().rev() {
48+
let mut node = ListNode::new(v);
49+
node.next = current;
50+
current = Some(Box::new(node));
51+
}
52+
current
53+
}
54+
55+
pub struct Solution {}
56+
57+
// submission codes start here
58+
59+
impl Solution {
60+
pub fn add_two_numbers(l1: Option<Box<ListNode>>, l2: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
61+
let (mut l1, mut l2) = (l1, l2);
62+
let mut curr = None;
63+
let (mut l1_end, mut l2_end, mut overflow) = (false, false, false);
64+
loop {
65+
let lhs = match l1 {
66+
Some(node) => { l1 = node.next; node.val },
67+
None => { l1_end = true; 0 },
68+
};
69+
let rhs = match l2 {
70+
Some(node) => { l2 = node.next; node.val },
71+
None => { l2_end = true; 0 }
72+
};
73+
// if l1, l2 end and there is not overflow from previous operation, return the result
74+
if l1_end && l2_end && !overflow {
75+
break Solution::reverse(curr);
76+
}
77+
let sum = lhs + rhs + if overflow { 1 } else { 0 };
78+
let sum = if sum >= 10 { overflow = true; sum - 10 } else { overflow = false; sum };
79+
let mut node = ListNode::new(sum);
80+
node.next = curr;
81+
curr = Some(Box::new(node));
82+
}
83+
}
84+
85+
fn reverse(l: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
86+
let mut prev = None;
87+
let mut current = l;
88+
while let Some(mut current_node_inner) = current {
89+
let next = current_node_inner.next;
90+
current_node_inner.next = prev;
91+
prev = Some(current_node_inner);
92+
current = next;
93+
}
94+
prev
95+
}
96+
}
97+
98+
// submission codes end
99+
100+
#[cfg(test)]
101+
mod tests {
102+
use super::*;
103+
104+
#[test]
105+
fn test_2() {
106+
assert_eq!(Solution::add_two_numbers(to_list(vec![2, 4, 3]), to_list(vec![5, 6, 4])), to_list(vec![7, 0, 8]));
107+
108+
assert_eq!(Solution::add_two_numbers(to_list(vec![9, 9, 9, 9]), to_list(vec![9, 9, 9, 9, 9, 9])), to_list(vec![8, 9, 9, 9, 0, 0, 1]));
109+
}
110+
}

0 commit comments

Comments
 (0)