Skip to content

Commit f21a7ed

Browse files
committed
add 10 mediums
1 parent 0ef8aec commit f21a7ed

File tree

7 files changed

+600
-225
lines changed

7 files changed

+600
-225
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rsleetcode"
3-
version = "0.0.2"
3+
version = "0.0.3"
44
edition = "2021"
55
repository = "https://github.com/LeetCode-Packages/Rust"
66
license-file = "LICENSE"

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# Rust
2+
23
Implementations of LeetCode problem solutions in Rust.

src/easy.rs

Lines changed: 26 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::utils;
1+
use super::utils::*;
22
use std::cell::RefCell;
33
use std::rc::Rc;
44

@@ -103,20 +103,20 @@ pub fn valid_parentheses(s: String) -> bool {
103103
}
104104

105105
pub fn merge_two_sorted_lists(
106-
list1: Option<Box<utils::ListNode>>,
107-
list2: Option<Box<utils::ListNode>>,
108-
) -> Option<Box<utils::ListNode>> {
106+
list1: Option<Box<ListNode>>,
107+
list2: Option<Box<ListNode>>,
108+
) -> Option<Box<ListNode>> {
109109
match (list1, list2) {
110110
(None, None) => None,
111111
(Some(n), None) | (None, Some(n)) => Some(n),
112112
(Some(list1), Some(list2)) => {
113113
if list1.val >= list2.val {
114-
Some(Box::new(utils::ListNode {
114+
Some(Box::new(ListNode {
115115
val: list2.val,
116116
next: merge_two_sorted_lists(Some(list1), list2.next),
117117
}))
118118
} else {
119-
Some(Box::new(utils::ListNode {
119+
Some(Box::new(ListNode {
120120
val: list1.val,
121121
next: merge_two_sorted_lists(list1.next, Some(list2)),
122122
}))
@@ -221,9 +221,7 @@ pub fn climbing_stairs(n: i32) -> i32 {
221221
(0..n).fold((1, 0), |(res, prev), _| (res + prev, res)).0
222222
}
223223

224-
pub fn remove_duplicates_from_sorted_list(
225-
head: Option<Box<utils::ListNode>>,
226-
) -> Option<Box<utils::ListNode>> {
224+
pub fn remove_duplicates_from_sorted_list(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
227225
head.map(|mut ln| {
228226
let mut cur = ln.as_mut();
229227
while let Some(next) = cur.next.as_mut() {
@@ -250,9 +248,9 @@ pub fn merge_sorted_array(nums1: &mut Vec<i32>, m: i32, nums2: &mut Vec<i32>, n:
250248
}
251249
}
252250

253-
pub fn binary_tree_inorder_traversal(root: Option<Rc<RefCell<utils::TreeNode>>>) -> Vec<i32> {
251+
pub fn binary_tree_inorder_traversal(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
254252
let mut curr = root;
255-
let mut stack = Vec::<Rc<RefCell<utils::TreeNode>>>::new();
253+
let mut stack = Vec::<Rc<RefCell<TreeNode>>>::new();
256254
let mut res = vec![];
257255

258256
while curr.is_some() || !stack.is_empty() {
@@ -270,14 +268,11 @@ pub fn binary_tree_inorder_traversal(root: Option<Rc<RefCell<utils::TreeNode>>>)
270268
res
271269
}
272270

273-
pub fn same_tree(
274-
p: Option<Rc<RefCell<utils::TreeNode>>>,
275-
q: Option<Rc<RefCell<utils::TreeNode>>>,
276-
) -> bool {
271+
pub fn same_tree(p: Option<Rc<RefCell<TreeNode>>>, q: Option<Rc<RefCell<TreeNode>>>) -> bool {
277272
p == q
278273
}
279274

280-
pub fn symmetric_tree(root: Option<Rc<RefCell<utils::TreeNode>>>) -> bool {
275+
pub fn symmetric_tree(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
281276
if root.is_none() {
282277
return true;
283278
}
@@ -301,7 +296,7 @@ pub fn symmetric_tree(root: Option<Rc<RefCell<utils::TreeNode>>>) -> bool {
301296
true
302297
}
303298

304-
pub fn maximum_depth_of_binary_tree(root: Option<Rc<RefCell<utils::TreeNode>>>) -> i32 {
299+
pub fn maximum_depth_of_binary_tree(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
305300
if root.is_none() {
306301
return 0;
307302
}
@@ -322,16 +317,14 @@ pub fn maximum_depth_of_binary_tree(root: Option<Rc<RefCell<utils::TreeNode>>>)
322317
max_depth
323318
}
324319

325-
pub fn convert_sorted_array_to_binary_search_tree(
326-
nums: Vec<i32>,
327-
) -> Option<Rc<RefCell<utils::TreeNode>>> {
320+
pub fn convert_sorted_array_to_binary_search_tree(nums: Vec<i32>) -> Option<Rc<RefCell<TreeNode>>> {
328321
let n = nums.len();
329322

330323
match n {
331324
0 => None,
332325
_ => {
333326
let m = n / 2;
334-
let mut node = utils::TreeNode::new(nums[m]);
327+
let mut node = TreeNode::new(nums[m]);
335328
node.left = convert_sorted_array_to_binary_search_tree(nums[..m].to_vec());
336329
node.right = convert_sorted_array_to_binary_search_tree(nums[m + 1..].to_vec());
337330

@@ -340,7 +333,7 @@ pub fn convert_sorted_array_to_binary_search_tree(
340333
}
341334
}
342335

343-
pub fn balanced_binary_tree(root: Option<Rc<RefCell<utils::TreeNode>>>) -> bool {
336+
pub fn balanced_binary_tree(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
344337
let mut dstack = Vec::new();
345338
let mut stack = Vec::new();
346339
stack.push((1 as i32, 0 as i32, false, false, root));
@@ -379,7 +372,7 @@ pub fn balanced_binary_tree(root: Option<Rc<RefCell<utils::TreeNode>>>) -> bool
379372
return true;
380373
}
381374

382-
pub fn minimum_depth_of_binary_tree(root: Option<Rc<RefCell<utils::TreeNode>>>) -> i32 {
375+
pub fn minimum_depth_of_binary_tree(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
383376
match root {
384377
Some(node) => {
385378
let left = minimum_depth_of_binary_tree(node.as_ref().borrow().left.clone());
@@ -393,14 +386,14 @@ pub fn minimum_depth_of_binary_tree(root: Option<Rc<RefCell<utils::TreeNode>>>)
393386
}
394387
}
395388

396-
pub fn path_sum(root: Option<Rc<RefCell<utils::TreeNode>>>, target_sum: i32) -> bool {
389+
pub fn path_sum(root: Option<Rc<RefCell<TreeNode>>>, target_sum: i32) -> bool {
397390
root.map_or(false, |root| match &*root.borrow() {
398-
&utils::TreeNode {
391+
&TreeNode {
399392
val,
400393
left: None,
401394
right: None,
402395
} => val == target_sum,
403-
&utils::TreeNode {
396+
&TreeNode {
404397
val,
405398
ref left,
406399
ref right,
@@ -430,11 +423,11 @@ pub fn single_number(nums: Vec<i32>) -> i32 {
430423

431424
// Linked List Cycle - not possible with current ListNode definition
432425

433-
pub fn binary_tree_preorder_traversal(root: Option<Rc<RefCell<utils::TreeNode>>>) -> Vec<i32> {
426+
pub fn binary_tree_preorder_traversal(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
434427
todo!();
435428
}
436429

437-
pub fn binary_tree_postorder_traversal(root: Option<Rc<RefCell<utils::TreeNode>>>) -> Vec<i32> {
430+
pub fn binary_tree_postorder_traversal(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
438431
todo!();
439432
}
440433

@@ -470,18 +463,15 @@ pub fn happy_number(n: i32) -> bool {
470463
todo!();
471464
}
472465

473-
pub fn remove_linked_list_elements(
474-
head: Option<Box<utils::ListNode>>,
475-
val: i32,
476-
) -> Option<Box<utils::ListNode>> {
466+
pub fn remove_linked_list_elements(head: Option<Box<ListNode>>, val: i32) -> Option<Box<ListNode>> {
477467
todo!();
478468
}
479469

480470
pub fn isomorphic_strings(s: String, t: String) -> bool {
481471
todo!();
482472
}
483473

484-
pub fn reverse_linked_list(head: Option<Box<utils::ListNode>>) -> Option<Box<utils::ListNode>> {
474+
pub fn reverse_linked_list(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
485475
todo!();
486476
}
487477

@@ -493,15 +483,13 @@ pub fn contains_duplicate_ii(nums: Vec<i32>, k: i32) -> bool {
493483
todo!();
494484
}
495485

496-
pub fn count_complete_tree_nodes(root: Option<Rc<RefCell<utils::TreeNode>>>) -> i32 {
486+
pub fn count_complete_tree_nodes(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
497487
todo!();
498488
}
499489

500490
// Implement Stack using Queues - not applicable
501491

502-
pub fn invert_binary_tree(
503-
root: Option<Rc<RefCell<utils::TreeNode>>>,
504-
) -> Option<Rc<RefCell<utils::TreeNode>>> {
492+
pub fn invert_binary_tree(root: Option<Rc<RefCell<TreeNode>>>) -> Option<Rc<RefCell<TreeNode>>> {
505493
todo!();
506494
}
507495

@@ -515,7 +503,7 @@ pub fn power_of_two(n: i32) -> bool {
515503

516504
// Implement Queue using Stacks - not applicable
517505

518-
pub fn palindrome_linked_list(head: Option<Box<utils::ListNode>>) -> bool {
506+
pub fn palindrome_linked_list(head: Option<Box<ListNode>>) -> bool {
519507
todo!();
520508
}
521509

0 commit comments

Comments
 (0)