Skip to content

Commit fe3de49

Browse files
committed
feat: 🎸 leetcode 1768
1 parent 1bf8234 commit fe3de49

40 files changed

+1001
-0
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
# leetcode_practice
2+
3+
- [] 141. Linked List Cycle
4+
- [] 104. Maximum Depth of Binary Tree stack solution

leet code/100. Same Tree/solution

526 KB
Binary file not shown.

leet code/100. Same Tree/solution.rs

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// Definition for a binary tree node.
2+
#[derive(Debug, PartialEq, Eq)]
3+
pub struct TreeNode {
4+
pub val: i32,
5+
pub left: Option<Rc<RefCell<TreeNode>>>,
6+
pub right: Option<Rc<RefCell<TreeNode>>>,
7+
}
8+
9+
impl TreeNode {
10+
#[inline]
11+
pub fn new(val: i32) -> Self {
12+
TreeNode {
13+
val,
14+
left: None,
15+
right: None,
16+
}
17+
}
18+
}
19+
20+
use std::rc::Rc;
21+
use std::cell::RefCell;
22+
23+
struct Solution;
24+
25+
impl Solution {
26+
pub fn is_same_tree(
27+
p: Option<Rc<RefCell<TreeNode>>>,
28+
q: Option<Rc<RefCell<TreeNode>>>
29+
) -> bool {
30+
match (p, q) {
31+
(None, None) => true,
32+
(None, Some(_)) | (Some(_), None) => false,
33+
(Some(ref node1), Some(ref node2)) => {
34+
if node1.borrow().val == node2.borrow().val {
35+
return Self::is_same_tree(node1.borrow().left.clone(), node2.borrow().left.clone())
36+
&& Self::is_same_tree(node1.borrow().right.clone(), node2.borrow().right.clone());
37+
} else {
38+
false
39+
}
40+
}
41+
}
42+
}
43+
}
44+
45+
fn main() {
46+
let node1_p = Rc::new(RefCell::new(TreeNode::new(2)));
47+
let node2_p = Rc::new(RefCell::new(TreeNode::new(3)));
48+
let root_p = Rc::new(RefCell::new(TreeNode {
49+
val: 1,
50+
left: Some(node1_p),
51+
right: Some(node2_p),
52+
}));
53+
54+
let node1_q = Rc::new(RefCell::new(TreeNode::new(2)));
55+
let node2_q = Rc::new(RefCell::new(TreeNode::new(3)));
56+
let root_q = Rc::new(RefCell::new(TreeNode {
57+
val: 1,
58+
left: Some(node1_q),
59+
right: Some(node2_q),
60+
}));
61+
62+
let result = Solution::is_same_tree(Some(root_p), Some(root_q));
63+
println!("Are the trees same? {:?}", result); // 输出: true
64+
}
65+
66+
#[cfg(test)]
67+
mod tests {
68+
use super::*;
69+
70+
#[test]
71+
fn test_is_same_tree() {
72+
let node1_p = Rc::new(RefCell::new(TreeNode::new(2)));
73+
let node2_p = Rc::new(RefCell::new(TreeNode::new(3)));
74+
let root_p = Rc::new(RefCell::new(TreeNode {
75+
val: 1,
76+
left: Some(node1_p),
77+
right: Some(node2_p),
78+
}));
79+
80+
let node1_q = Rc::new(RefCell::new(TreeNode::new(2)));
81+
let node2_q = Rc::new(RefCell::new(TreeNode::new(3)));
82+
let root_q = Rc::new(RefCell::new(TreeNode {
83+
val: 1,
84+
left: Some(node1_q),
85+
right: Some(node2_q),
86+
}));
87+
88+
assert!(Solution::is_same_tree(Some(root_p), Some(root_q)));
89+
90+
let node1_p = Rc::new(RefCell::new(TreeNode::new(2)));
91+
let root_p = Rc::new(RefCell::new(TreeNode {
92+
val: 1,
93+
left: Some(node1_p),
94+
right: None,
95+
}));
96+
97+
let node1_q = Rc::new(RefCell::new(TreeNode::new(2)));
98+
let root_q = Rc::new(RefCell::new(TreeNode {
99+
val: 1,
100+
left: None,
101+
right: Some(node1_q),
102+
}));
103+
104+
assert!(!Solution::is_same_tree(Some(root_p), Some(root_q)));
105+
106+
let root_p: Option<Rc<RefCell<TreeNode>>> = None;
107+
let root_q: Option<Rc<RefCell<TreeNode>>> = None;
108+
assert!(Solution::is_same_tree(root_p, root_q));
109+
}
110+
}
1.15 MB
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
use std::rc::Rc;
2+
use std::cell::RefCell;
3+
4+
// Definition for a binary tree node.
5+
#[derive(PartialEq, Eq, Clone, Debug)]
6+
pub struct TreeNode {
7+
pub val: i32,
8+
pub left: Option<Rc<RefCell<TreeNode>>>,
9+
pub right: Option<Rc<RefCell<TreeNode>>>,
10+
}
11+
12+
impl TreeNode {
13+
#[inline]
14+
pub fn new(val: i32) -> Self {
15+
TreeNode {
16+
val,
17+
left: None,
18+
right: None,
19+
}
20+
}
21+
}
22+
23+
struct Solution;
24+
25+
impl Solution {
26+
pub fn max_depth(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
27+
match root {
28+
Some(n) => {
29+
let node = n.borrow();
30+
let left = Self::max_depth(node.left.clone());
31+
let right = Self::max_depth(node.right.clone());
32+
if left > right {
33+
left + 1
34+
} else {
35+
right + 1
36+
}
37+
}
38+
None => 0,
39+
}
40+
}
41+
}
42+
43+
fn main() {
44+
let node15 = Rc::new(RefCell::new(TreeNode::new(15)));
45+
let node7 = Rc::new(RefCell::new(TreeNode::new(7)));
46+
let node20 = Rc::new(RefCell::new(TreeNode {
47+
val: 20,
48+
left: Some(node15),
49+
right: Some(node7),
50+
}));
51+
let node9 = Rc::new(RefCell::new(TreeNode::new(9)));
52+
let root = Rc::new(RefCell::new(TreeNode {
53+
val: 3,
54+
left: Some(node9),
55+
right: Some(node20),
56+
}));
57+
58+
let result = Solution::max_depth(Some(root));
59+
println!("Max depth: {:?}", result);
60+
}
61+
62+
#[cfg(test)]
63+
mod tests {
64+
use super::*;
65+
66+
#[test]
67+
fn test_max_depth() {
68+
// 测试普通二叉树
69+
let node15 = Rc::new(RefCell::new(TreeNode::new(15)));
70+
let node7 = Rc::new(RefCell::new(TreeNode::new(7)));
71+
let node20 = Rc::new(RefCell::new(TreeNode {
72+
val: 20,
73+
left: Some(node15),
74+
right: Some(node7),
75+
}));
76+
let node9 = Rc::new(RefCell::new(TreeNode::new(9)));
77+
let root = Rc::new(RefCell::new(TreeNode {
78+
val: 3,
79+
left: Some(node9),
80+
right: Some(node20),
81+
}));
82+
assert_eq!(Solution::max_depth(Some(root)), 3);
83+
84+
// 测试单个节点
85+
let root = Rc::new(RefCell::new(TreeNode::new(1)));
86+
assert_eq!(Solution::max_depth(Some(root)), 1);
87+
88+
// 测试空树
89+
let root: Option<Rc<RefCell<TreeNode>>> = None;
90+
assert_eq!(Solution::max_depth(root), 0);
91+
}
92+
}
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
![image](./image/img.png)
3+
4+
### ex1
5+
6+
> input: word1 = "abc", word2 = "pqr"
7+
8+
> output: "apbqcr"
9+
10+
### ex2
11+
12+
> input: word1 = "ab", word2 = "pqrs"
13+
14+
> output: "apbqrs"
15+
16+
## 思路
17+
使用兩個指標分別紀錄要合併字元的位置,合併完後往右走一格,直到全部都合併完成。
Loading
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn merge_alternately(word1: String, word2: String) -> String {
5+
// let mut rs = String::new(); runtime beats 68%
6+
let mut rs = String::with_capacity(word1.len() + word2.len());//runtime beats 100%
7+
let mut w1_ptr = 0;
8+
let mut w2_ptr = 0;
9+
loop{
10+
if w1_ptr<word1.len() && w2_ptr<word2.len() {
11+
rs.push_str(&word1[w1_ptr..w1_ptr+1]);
12+
rs.push_str(&word2[w2_ptr..w2_ptr+1]);
13+
w1_ptr +=1;
14+
w2_ptr +=1;
15+
} else if w1_ptr<word1.len() {
16+
rs.push_str(&word1[w1_ptr..w1_ptr+1]);
17+
w1_ptr +=1;
18+
} else if w2_ptr<word2.len(){
19+
rs.push_str(&word2[w2_ptr..w2_ptr+1]);
20+
w2_ptr +=1;
21+
} else{
22+
break;
23+
}
24+
}
25+
rs
26+
}
27+
}
28+
29+
fn main() {
30+
let word1 = String::from("abc");
31+
let word2 = String::from("pqr");
32+
let result = Solution::merge_alternately(word1, word2);
33+
println!("Merge alternately: {:?}", result);
34+
}
35+
36+
#[cfg(test)]
37+
mod tests {
38+
use super::*;
39+
40+
#[test]
41+
fn test_merge_alternately() {
42+
assert_eq!(Solution::merge_alternately(String::from("abc"), String::from("pqr")), String::from("apbqcr"));
43+
assert_eq!(Solution::merge_alternately(String::from("ab"), String::from("pq")), String::from("apbq"));
44+
assert_eq!(Solution::merge_alternately(String::from("a"), String::from("b")), String::from("ab"));
45+
assert_eq!(Solution::merge_alternately(String::from("ab"), String::from("c")), String::from("acb"));
46+
assert_eq!(Solution::merge_alternately(String::from("a"), String::from("")), String::from("a"));
47+
assert_eq!(Solution::merge_alternately(String::from(""), String::from("b")), String::from("b"));
48+
}
49+
}
Binary file not shown.
525 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#[derive(PartialEq, Eq, Clone, Debug)]
2+
pub struct ListNode {
3+
pub val: i32,
4+
pub next: Option<Box<ListNode>>,
5+
}
6+
7+
impl ListNode {
8+
#[inline]
9+
fn new(val: i32) -> Self {
10+
ListNode {
11+
next: None,
12+
val,
13+
}
14+
}
15+
}
16+
17+
struct Solution;
18+
19+
impl Solution {
20+
pub fn reverse_list(mut head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
21+
let mut re_list: Option<Box<ListNode>> = None;
22+
while let Some(mut current) = head {
23+
head = current.next;
24+
current.next = re_list;
25+
re_list = Some(current);
26+
}
27+
re_list
28+
}
29+
}
30+
31+
fn main() {
32+
// 创建一个链表: 1 -> 2 -> 3 -> 4 -> 5
33+
let list = Some(Box::new(ListNode {
34+
val: 1,
35+
next: Some(Box::new(ListNode {
36+
val: 2,
37+
next: Some(Box::new(ListNode {
38+
val: 3,
39+
next: Some(Box::new(ListNode {
40+
val: 4,
41+
next: Some(Box::new(ListNode {
42+
val: 5,
43+
next: None,
44+
})),
45+
})),
46+
})),
47+
})),
48+
}));
49+
50+
// 反转链表
51+
let result = Solution::reverse_list(list);
52+
53+
// 打印反转后的链表
54+
println!("{:?}", result); // 输出: Some(ListNode { val: 5, next: Some(ListNode { val: 4, next: Some(ListNode { val: 3, next: Some(ListNode { val: 2, next: Some(ListNode { val: 1, next: None }) }) }) }) })
55+
}
56+
57+
#[cfg(test)]
58+
mod tests {
59+
use super::*;
60+
61+
#[test]
62+
fn test_reverse_list() {
63+
let list = Some(Box::new(ListNode {
64+
val: 1,
65+
next: Some(Box::new(ListNode {
66+
val: 2,
67+
next: Some(Box::new(ListNode {
68+
val: 3,
69+
next: Some(Box::new(ListNode {
70+
val: 4,
71+
next: Some(Box::new(ListNode {
72+
val: 5,
73+
next: None,
74+
})),
75+
})),
76+
})),
77+
})),
78+
}));
79+
let reversed = Some(Box::new(ListNode {
80+
val: 5,
81+
next: Some(Box::new(ListNode {
82+
val: 4,
83+
next: Some(Box::new(ListNode {
84+
val: 3,
85+
next: Some(Box::new(ListNode {
86+
val: 2,
87+
next: Some(Box::new(ListNode {
88+
val: 1,
89+
next: None,
90+
})),
91+
})),
92+
})),
93+
})),
94+
}));
95+
assert_eq!(Solution::reverse_list(list), reversed);
96+
97+
let list = None;
98+
let reversed = None;
99+
assert_eq!(Solution::reverse_list(list), reversed);
100+
101+
let list = Some(Box::new(ListNode::new(1)));
102+
let reversed = Some(Box::new(ListNode::new(1)));
103+
assert_eq!(Solution::reverse_list(list), reversed);
104+
}
105+
}
Binary file not shown.
525 KB
Binary file not shown.

0 commit comments

Comments
 (0)