Skip to content

Commit

Permalink
Solve leetcode 22
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielseibel1 committed Oct 3, 2023
1 parent 692bffc commit da59f04
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 32 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
- uses: actions/checkout@v3
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[![Rust](https://github.com/gabrielseibel1/leetcoderust/actions/workflows/rust.yml/badge.svg)](https://github.com/gabrielseibel1/leetcoderust/actions/workflows/rust.yml)

# Leetcode Rust

Leetcode solutions in rust
47 changes: 24 additions & 23 deletions src/lc143.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub struct ListNode {

impl ListNode {
#[inline]
#[allow(dead_code)]
fn new(val: i32) -> Self {
ListNode {
next: None,
Expand Down Expand Up @@ -71,40 +72,40 @@ fn reverse(list: &mut List) {
}
}

fn print(list: &List) {
match list {
None => println!("->🏁"),
Some(node) => {
print!("->{}", node.val);
print(&node.next);
}
}
}


fn len(list: &List) -> i32 {
match list {
None => 0,
Some(node) => 1 + len(&node.next),
}
}

fn eq(l: &List, s: &[i32]) -> bool {
if s.is_empty() {
return l.is_none();
}
match l {
None => s.len() == 0,
Some(ln) => {
ln.val.eq(&s[0]) && eq(&ln.next, &s[1..s.len()])
}
}
}

#[allow(dead_code)]
#[cfg(test)]
mod tests {
use super::*;

fn eq(l: &List, s: &[i32]) -> bool {
if s.is_empty() {
return l.is_none();
}
match l {
None => s.is_empty(),
Some(ln) => {
ln.val.eq(&s[0]) && eq(&ln.next, &s[1..s.len()])
}
}
}

fn print(list: &List) {
match list {
None => println!("->🏁"),
Some(node) => {
print!("->{}", node.val);
print(&node.next);
}
}
}

#[test]
fn test_reorder_list() {
let mut list = Some::<Box<ListNode>>(Box::new(
Expand Down
1 change: 0 additions & 1 deletion src/lc20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ pub fn is_valid(s: String) -> bool {

#[cfg(test)]
mod tests {

use super::*;

#[test]
Expand Down
5 changes: 2 additions & 3 deletions src/lc206.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use log::warn;

// Definition for singly-linked list.
// #[derive(PartialEq, Eq, Clone, Debug)]
pub struct ListNode {
Expand All @@ -9,6 +7,7 @@ pub struct ListNode {

impl ListNode {
#[inline]
#[allow(dead_code)]
fn new(val: i32) -> Self {
ListNode {
next: None,
Expand All @@ -22,7 +21,7 @@ pub fn reverse_list(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
let mut next: Option<Box<ListNode>> = head;
loop {
if next.is_none() {
break
break;
}
let mut next_node: Box<ListNode> = next.take().unwrap();
let fwd = next_node.next.take();
Expand Down
22 changes: 22 additions & 0 deletions src/lc22.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
pub fn generate_parenthesis(n: i32) -> Vec<String> {
let mut v = vec![];
gen(n, &mut String::from(""), &mut v, 0, 0);
v
}

fn gen(n: i32, s: &mut String, set: &mut Vec<String>, o: i32, c: i32) {
if o == n && c == n {
set.push(s.clone());
return;
}
if o > c && c < n {
s.push(')');
gen(n, s, set, o, c + 1);
s.pop();
}
if o < n {
s.push('(');
gen(n, s, set, o + 1, c);
s.pop();
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ pub mod lc242;
pub mod lc74;
pub mod lc143;
pub mod lc206;
pub mod lc22;

0 comments on commit da59f04

Please sign in to comment.