Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit db56909

Browse files
committed
update
1 parent da5544b commit db56909

File tree

3 files changed

+118
-3
lines changed

3 files changed

+118
-3
lines changed

src/problem/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
mod p0001_two_sum;
2+
mod p0002_add_two_numbers;

src/problem/p0001_two_sum.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,16 @@ use std::collections::HashMap;
4444
impl Solution {
4545
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
4646
let ans: Vec<i32> = vec![];
47-
let mut exis:HashMap<i32, bool> = HashMap::with_capacity(nums.len());
47+
let mut exist = HashMap::with_capacity(nums.len());
4848

4949
for (index, num) in nums.iter().enumerate() {
50-
50+
match exist.get(&(target - num)) {
51+
None => {
52+
exist.insert(num, index);
53+
}
54+
Some(idx) => return vec![*idx as i32, index as i32],
55+
}
5156
}
52-
5357
ans
5458
}
5559
}

src/problem/p0002_add_two_numbers.rs

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/**
2+
* [0002] Add Two Numbers
3+
*
4+
* You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.
5+
* You may assume the two numbers do not contain any leading zero, except the number 0 itself.
6+
*
7+
* Example 1:
8+
* <img alt="" src="https://assets.leetcode.com/uploads/2020/10/02/addtwonumber1.jpg" style="width: 483px; height: 342px;" />
9+
* Input: l1 = [2,4,3], l2 = [5,6,4]
10+
* Output: [7,0,8]
11+
* Explanation: 342 + 465 = 807.
12+
*
13+
* Example 2:
14+
*
15+
* Input: l1 = [0], l2 = [0]
16+
* Output: [0]
17+
*
18+
* Example 3:
19+
*
20+
* Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
21+
* Output: [8,9,9,9,0,0,0,1]
22+
*
23+
*
24+
* Constraints:
25+
*
26+
* The number of nodes in each linked list is in the range [1, 100].
27+
* 0 <= Node.val <= 9
28+
* It is guaranteed that the list represents a number that does not have leading zeros.
29+
*
30+
*/
31+
pub struct Solution {}
32+
use crate::util::linked_list::{ListNode, to_list};
33+
34+
// problem: https://leetcode.com/problems/add-two-numbers/
35+
// discuss: https://leetcode.com/problems/add-two-numbers/discuss/?currentPage=1&orderBy=most_votes&query=
36+
37+
// submission codes start here
38+
39+
// Definition for singly-linked list.
40+
// #[derive(PartialEq, Eq, Clone, Debug)]
41+
// pub struct ListNode {
42+
// pub val: i32,
43+
// pub next: Option<Box<ListNode>>
44+
// }
45+
//
46+
// impl ListNode {
47+
// #[inline]
48+
// fn new(val: i32) -> Self {
49+
// ListNode {
50+
// next: None,
51+
// val
52+
// }
53+
// }
54+
// }
55+
impl Solution {
56+
pub fn add_two_numbers(l1: Option<Box<ListNode>>, l2: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
57+
let (mut l1, mut l2) = (l1, l2);
58+
let mut dummy_head = Some(Box::new(ListNode::new(0)));
59+
let mut cur = &dummy_head;
60+
let (mut l1_end, mut l2_end, mut overflow) = (false, false, false);
61+
loop {
62+
let v1 = match l1 {
63+
None => {
64+
l1_end = true;
65+
0
66+
}
67+
Some(node) => {
68+
l1 = node.next;
69+
node.val
70+
}
71+
};
72+
let v2 = match l2 {
73+
None => {
74+
l2_end = true;
75+
0
76+
}
77+
Some(node) => {
78+
l2 = node.next;
79+
node.val
80+
}
81+
};
82+
83+
if (l1_end && l2_end && !overflow) {
84+
return dummy_head.unwrap().next;
85+
}
86+
let sum = v1 + v2 + if overflow {1} else {0};
87+
let sum = if sum >= 0 {
88+
overflow = true;
89+
sum - 10
90+
} else {
91+
overflow = false;
92+
sum
93+
};
94+
95+
cur.a
96+
}
97+
Some(Box::new(ListNode::new(0)))
98+
}
99+
}
100+
101+
// submission codes end
102+
103+
#[cfg(test)]
104+
mod tests {
105+
use super::*;
106+
107+
#[test]
108+
fn test_0002() {
109+
}
110+
}

0 commit comments

Comments
 (0)