Skip to content

Add 10 mediums #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# How to Contribute

If you want to implement new methods, follow this general format.

- Method names must match the LeetCode problem name in snake case, e.g. Two Sum -> two_sum
- Method definitions must match the LeetCode definitions provided, types and all (excluding externally defined types like ListNode)
- Test names must be named the same, prefixed with `test_`
- Test cases must match the test cases given by LeetCode

Below are features/documentation that may be implemented in future release, but are not deemed a priority as of now.

- Method documentation
- Assertions to validate method inputs
- Extensive test cases/full code coverage
7 changes: 7 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Fixes # .

Changes proposed in this pull request:

*
*
*
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ name: Cargo Build & Test

on:
push:
branches:
- main
pull_request:
branches:
- main

env:
CARGO_TERM_COLOR: always
Expand Down
22 changes: 22 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Publish to crates.io

on:
push:
branches:
- main

env:
CARGO_TERM_COLOR: always

jobs:
publish:
name: Rust project - latest
runs-on: ubuntu-latest
strategy:
matrix:
toolchain:
- stable
steps:
- uses: actions/checkout@v4
- run: rustup update ${{ matrix.toolchain }} && rustup default ${{ matrix.toolchain }}
- run: cargo publish --verbose
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rsleetcode"
version = "0.0.2"
version = "0.0.3"
edition = "2021"
repository = "https://github.com/LeetCode-Packages/Rust"
license-file = "LICENSE"
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# Rust

Implementations of LeetCode problem solutions in Rust.
64 changes: 26 additions & 38 deletions src/easy.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::utils;
use super::utils::*;
use std::cell::RefCell;
use std::rc::Rc;

Expand Down Expand Up @@ -103,20 +103,20 @@ pub fn valid_parentheses(s: String) -> bool {
}

pub fn merge_two_sorted_lists(
list1: Option<Box<utils::ListNode>>,
list2: Option<Box<utils::ListNode>>,
) -> Option<Box<utils::ListNode>> {
list1: Option<Box<ListNode>>,
list2: Option<Box<ListNode>>,
) -> Option<Box<ListNode>> {
match (list1, list2) {
(None, None) => None,
(Some(n), None) | (None, Some(n)) => Some(n),
(Some(list1), Some(list2)) => {
if list1.val >= list2.val {
Some(Box::new(utils::ListNode {
Some(Box::new(ListNode {
val: list2.val,
next: merge_two_sorted_lists(Some(list1), list2.next),
}))
} else {
Some(Box::new(utils::ListNode {
Some(Box::new(ListNode {
val: list1.val,
next: merge_two_sorted_lists(list1.next, Some(list2)),
}))
Expand Down Expand Up @@ -221,9 +221,7 @@ pub fn climbing_stairs(n: i32) -> i32 {
(0..n).fold((1, 0), |(res, prev), _| (res + prev, res)).0
}

pub fn remove_duplicates_from_sorted_list(
head: Option<Box<utils::ListNode>>,
) -> Option<Box<utils::ListNode>> {
pub fn remove_duplicates_from_sorted_list(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
head.map(|mut ln| {
let mut cur = ln.as_mut();
while let Some(next) = cur.next.as_mut() {
Expand All @@ -250,9 +248,9 @@ pub fn merge_sorted_array(nums1: &mut Vec<i32>, m: i32, nums2: &mut Vec<i32>, n:
}
}

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

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

pub fn same_tree(
p: Option<Rc<RefCell<utils::TreeNode>>>,
q: Option<Rc<RefCell<utils::TreeNode>>>,
) -> bool {
pub fn same_tree(p: Option<Rc<RefCell<TreeNode>>>, q: Option<Rc<RefCell<TreeNode>>>) -> bool {
p == q
}

pub fn symmetric_tree(root: Option<Rc<RefCell<utils::TreeNode>>>) -> bool {
pub fn symmetric_tree(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
if root.is_none() {
return true;
}
Expand All @@ -301,7 +296,7 @@ pub fn symmetric_tree(root: Option<Rc<RefCell<utils::TreeNode>>>) -> bool {
true
}

pub fn maximum_depth_of_binary_tree(root: Option<Rc<RefCell<utils::TreeNode>>>) -> i32 {
pub fn maximum_depth_of_binary_tree(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
if root.is_none() {
return 0;
}
Expand All @@ -322,16 +317,14 @@ pub fn maximum_depth_of_binary_tree(root: Option<Rc<RefCell<utils::TreeNode>>>)
max_depth
}

pub fn convert_sorted_array_to_binary_search_tree(
nums: Vec<i32>,
) -> Option<Rc<RefCell<utils::TreeNode>>> {
pub fn convert_sorted_array_to_binary_search_tree(nums: Vec<i32>) -> Option<Rc<RefCell<TreeNode>>> {
let n = nums.len();

match n {
0 => None,
_ => {
let m = n / 2;
let mut node = utils::TreeNode::new(nums[m]);
let mut node = TreeNode::new(nums[m]);
node.left = convert_sorted_array_to_binary_search_tree(nums[..m].to_vec());
node.right = convert_sorted_array_to_binary_search_tree(nums[m + 1..].to_vec());

Expand All @@ -340,7 +333,7 @@ pub fn convert_sorted_array_to_binary_search_tree(
}
}

pub fn balanced_binary_tree(root: Option<Rc<RefCell<utils::TreeNode>>>) -> bool {
pub fn balanced_binary_tree(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
let mut dstack = Vec::new();
let mut stack = Vec::new();
stack.push((1 as i32, 0 as i32, false, false, root));
Expand Down Expand Up @@ -379,7 +372,7 @@ pub fn balanced_binary_tree(root: Option<Rc<RefCell<utils::TreeNode>>>) -> bool
return true;
}

pub fn minimum_depth_of_binary_tree(root: Option<Rc<RefCell<utils::TreeNode>>>) -> i32 {
pub fn minimum_depth_of_binary_tree(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
match root {
Some(node) => {
let left = minimum_depth_of_binary_tree(node.as_ref().borrow().left.clone());
Expand All @@ -393,14 +386,14 @@ pub fn minimum_depth_of_binary_tree(root: Option<Rc<RefCell<utils::TreeNode>>>)
}
}

pub fn path_sum(root: Option<Rc<RefCell<utils::TreeNode>>>, target_sum: i32) -> bool {
pub fn path_sum(root: Option<Rc<RefCell<TreeNode>>>, target_sum: i32) -> bool {
root.map_or(false, |root| match &*root.borrow() {
&utils::TreeNode {
&TreeNode {
val,
left: None,
right: None,
} => val == target_sum,
&utils::TreeNode {
&TreeNode {
val,
ref left,
ref right,
Expand Down Expand Up @@ -430,11 +423,11 @@ pub fn single_number(nums: Vec<i32>) -> i32 {

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

pub fn binary_tree_preorder_traversal(root: Option<Rc<RefCell<utils::TreeNode>>>) -> Vec<i32> {
pub fn binary_tree_preorder_traversal(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
todo!();
}

pub fn binary_tree_postorder_traversal(root: Option<Rc<RefCell<utils::TreeNode>>>) -> Vec<i32> {
pub fn binary_tree_postorder_traversal(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
todo!();
}

Expand Down Expand Up @@ -470,18 +463,15 @@ pub fn happy_number(n: i32) -> bool {
todo!();
}

pub fn remove_linked_list_elements(
head: Option<Box<utils::ListNode>>,
val: i32,
) -> Option<Box<utils::ListNode>> {
pub fn remove_linked_list_elements(head: Option<Box<ListNode>>, val: i32) -> Option<Box<ListNode>> {
todo!();
}

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

pub fn reverse_linked_list(head: Option<Box<utils::ListNode>>) -> Option<Box<utils::ListNode>> {
pub fn reverse_linked_list(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
todo!();
}

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

pub fn count_complete_tree_nodes(root: Option<Rc<RefCell<utils::TreeNode>>>) -> i32 {
pub fn count_complete_tree_nodes(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
todo!();
}

// Implement Stack using Queues - not applicable

pub fn invert_binary_tree(
root: Option<Rc<RefCell<utils::TreeNode>>>,
) -> Option<Rc<RefCell<utils::TreeNode>>> {
pub fn invert_binary_tree(root: Option<Rc<RefCell<TreeNode>>>) -> Option<Rc<RefCell<TreeNode>>> {
todo!();
}

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

// Implement Queue using Stacks - not applicable

pub fn palindrome_linked_list(head: Option<Box<utils::ListNode>>) -> bool {
pub fn palindrome_linked_list(head: Option<Box<ListNode>>) -> bool {
todo!();
}

Expand Down
Loading