Skip to content

Commit 72522d4

Browse files
committed
0002
1 parent cc1748b commit 72522d4

File tree

4 files changed

+40
-86
lines changed

4 files changed

+40
-86
lines changed

Algorithms/0002.add_two_numbers/add_two_numbers.rs

Lines changed: 29 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,82 +2,53 @@ use std::cmp;
22
// Definition for singly-linked list.
33
#[derive(PartialEq, Eq, Clone, Debug)]
44
pub struct ListNode {
5-
pub val: i32,
6-
pub next: Option<Box<ListNode>>
5+
pub val: i32,
6+
pub next: Option<Box<ListNode>>,
77
}
88

99
impl ListNode {
10-
#[inline]
11-
fn new(val: i32) -> Self {
12-
ListNode {
13-
next: None,
14-
val
10+
#[inline]
11+
pub fn new(val: i32) -> Self { // ?? leetcode is private
12+
ListNode { next: None, val }
1513
}
16-
}
14+
}
15+
16+
pub struct Solution {
1717
}
1818

1919
impl Solution {
20-
pub fn add_two_numbers(l1: Option<Box<ListNode>>, l2: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
21-
let mut i1 = l1;
22-
let mut i2 = l2;
20+
pub fn add_two_numbers(
21+
l1: Option<Box<ListNode>>,
22+
l2: Option<Box<ListNode>>,
23+
) -> Option<Box<ListNode>> {
24+
let mut n1 = l1;
25+
let mut n2 = l2;
2326
let mut overflow_value = 0;
27+
let mut vec = Vec::new();
2428

25-
while i1.is_some() || i2.is_some() {
26-
let v1 = i1.unwrap_or( Box::new(ListNode::new(0)) );
27-
let v2 = i2.unwrap_or( Box::new(ListNode::new(0)) );
29+
while n1.is_some() || n2.is_some() || overflow_value > 0 {
30+
let v1 = n1.unwrap_or(Box::new(ListNode::new(0)));
31+
let v2 = n2.unwrap_or(Box::new(ListNode::new(0)));
2832

2933
let mut v = v1.val + v2.val + overflow_value;
3034
overflow_value = v / 10;
3135
v = v % 10;
3236

33-
results.next.get_or_insert(Box::new(ListNode::new(v)));
34-
curr = &mut results.next;
37+
vec.push(v);
3538

36-
i1 = v1.next;
37-
i2 = v2.next;
38-
println!( "{:?}", v);
39+
n1 = v1.next;
40+
n2 = v2.next;
3941
}
4042

41-
42-
43-
// println!( "{:?}", i1.val);
44-
45-
46-
// let i2 = i1.next.unwrap_or( Box::new(ListNode::new(0)));
47-
48-
// println!( "{:?}", i2.val);
49-
50-
// let i3 = i2.next.unwrap_or( Box::new(ListNode::new(0)));
51-
52-
// println!( "{:?}", i3.val);
43+
let mut vec_last_index = vec.len() - 1;
44+
let mut results = Some(Box::new(ListNode::new(vec[vec_last_index])));
5345

54-
// let i4 = i3.next.unwrap_or( Box::new(ListNode::new(0)));
55-
56-
// println!( "{:?}", i4.val);
57-
58-
// let mut results: Some;
59-
// let mut i1 = l1;
60-
// let mut i2 = l2;
61-
62-
// let mut overfalow_value: i32 = 0;
63-
64-
// while i1.is_some() || i2.is_some() {
65-
// let mut v1 = i1.unwrap_or(0);
66-
// let mut v2 = i2.unwrap_or(0);
67-
68-
// let mut v = v1 + v2 + overflow_value;
69-
// overflow_value = v / 10;
70-
// v = v % 10;
71-
72-
// results.next.push_back(Some(Box::new(ListNode::new(v))));
73-
74-
// i1 = iter1.next;
75-
// i2 = iter2.next;
76-
// }
77-
// return results;
46+
for i in (0..vec_last_index).rev() {
47+
let mut ln = ListNode::new(vec[i]);
48+
ln.next = results;
49+
results = Some(Box::new(ln));
50+
}
7851

79-
return None;
52+
return results;
8053
}
8154
}
82-
83-
pub struct Solution {}

Algorithms/0002.add_two_numbers/add_two_numbers_test.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
mod add_two_numbers;
2-
mod list_node;
32
use add_two_numbers::Solution;
4-
use list_node::ListNode;
3+
use add_two_numbers::ListNode;
54

65
fn main() {
76
let arr1 = [9, 4, 5];
8-
let arr2 = [3, 4, 5, 5];
7+
let arr2 = [3, 4, 5];
98

109
let arr1_last_index = arr1.len() - 1;
1110
let arr2_last_index = arr2.len() - 1;

Algorithms/0002.add_two_numbers/list_node.rs

Lines changed: 0 additions & 16 deletions
This file was deleted.

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ leetcode in rust lang.
1616

1717
## Answer
1818

19-
|Order|Topic|Difficult|Runtime|Memory|Collection|
19+
|Order|Topic|Difficult|Runtime(ms)|Memory(MB)|Collection|
2020
|:-:|:-|:-:|:-:|:-:|---|
21-
|[1](https://leetcode.com/problems/two-sum/)|[Two Sum](./Algorithms/0001.two_sum)|Easy|0 ms|1.6 MB||
22-
|[2](https://leetcode.com/problems/add-two-numbers/)|[Add Two Numbers](./Algorithms/0002.add_two_numbers)|Medium||||
23-
|[3](https://leetcode.com/problems/longest-substring-without-repeating-characters/)|[Longest Substring Without Repeating Characters](./Algorithms/0003.longest_substring_without_repeating_characters)|Medium||||
24-
|[4](https://leetcode.com/problems/median-of-two-sorted-arrays/)|[Median of Two Sorted Arrays](./Algorithms/0004.median_of_two_sorted_arrays)|Hard|0 ms|2.1 MB||
25-
|[13](https://leetcode.com/problems/roman-to-integer/)|[Roman to Integer](./Algorithms/0013.roman_to_integer)|Easy|0 ms|1.9 MB||
26-
|[70]()|[Climbing Stairs](./Algorithms/0070.climbing_stairs)|Easy|0 ms|2.1 MB||
27-
|[88]()|[Merge Sorted Array](./Algorithms/0088.merge_sorted_array)|Easy|0 ms|2.2 MB||
28-
|[118]()|[Pascal's Triangle](./Algorithms/0118.pascals_triangle)|Easy|0 ms|2.1 MB||
21+
|[1](https://leetcode.com/problems/two-sum/)|[Two Sum](./Algorithms/0001.two_sum)|<font color=#00af9b>Easy</font>|0|1.6||
22+
|[2](https://leetcode.com/problems/add-two-numbers/)|[Add Two Numbers](./Algorithms/0002.add_two_numbers)|<font color=#ffb800>Medium</font>|0|2.2||
23+
|[3](https://leetcode.com/problems/longest-substring-without-repeating-characters/)|[Longest Substring Without Repeating Characters](./Algorithms/0003.longest_substring_without_repeating_characters)|<font color=#ffb800>Medium</font>||||
24+
|[4](https://leetcode.com/problems/median-of-two-sorted-arrays/)|[Median of Two Sorted Arrays](./Algorithms/0004.median_of_two_sorted_arrays)|<font color=#ff2d55>Hard</font>|0|2.1||
25+
|[13](https://leetcode.com/problems/roman-to-integer/)|[Roman to Integer](./Algorithms/0013.roman_to_integer)|<font color=#00af9b>Easy</font>|0|1.9||
26+
|[70]()|[Climbing Stairs](./Algorithms/0070.climbing_stairs)|<font color=#00af9b>Easy</font>|0|2.1||
27+
|[88]()|[Merge Sorted Array](./Algorithms/0088.merge_sorted_array)|<font color=#00af9b>Easy</font>|0|2.2||
28+
|[118]()|[Pascal's Triangle](./Algorithms/0118.pascals_triangle)|<font color=#00af9b>Easy</font>|0|2.1||

0 commit comments

Comments
 (0)