Skip to content

Commit d5cf820

Browse files
committed
1 parent 121b736 commit d5cf820

File tree

3 files changed

+190
-0
lines changed

3 files changed

+190
-0
lines changed

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ mod n0015_3sum;
1616
mod n0016_3sum_closest;
1717
mod n0017_letter_combinations_of_a_phone_number;
1818
mod n0018_4sum;
19+
mod n0019_remove_nth_node_from_end_of_list;
20+
mod n0020_valid_parentheses;
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
2+
/**
3+
* [19] Remove Nth Node From End of List
4+
*
5+
* Given a linked list, remove the n-th node from the end of list and return its head.
6+
*
7+
* Example:
8+
*
9+
*
10+
* Given linked list: 1->2->3->4->5, and n = 2.
11+
*
12+
* After removing the second node from the end, the linked list becomes 1->2->3->5.
13+
*
14+
*
15+
* Note:
16+
*
17+
* Given n will always be valid.
18+
*
19+
* Follow up:
20+
*
21+
* Could you do this in one pass?
22+
*
23+
*/
24+
pub struct Solution {}
25+
26+
// submission codes start here
27+
28+
// Definition for singly-linked list.
29+
#[derive(PartialEq, Eq, Debug)]
30+
pub struct ListNode {
31+
pub val: i32,
32+
pub next: Option<Box<ListNode>>
33+
}
34+
35+
impl ListNode {
36+
#[inline]
37+
fn new(val: i32) -> Self {
38+
ListNode {
39+
next: None,
40+
val
41+
}
42+
}
43+
}
44+
45+
// one pass (two pointer runner pattern) cannot make borrow checker happy
46+
// but two pass don't takes longer time
47+
impl Solution {
48+
pub fn remove_nth_from_end(head: Option<Box<ListNode>>, n: i32) -> Option<Box<ListNode>> {
49+
let mut dummy_head = Some(Box::new(ListNode {
50+
val: 0, next: head,
51+
}));
52+
let mut len = 0;
53+
{
54+
let mut p = dummy_head.as_ref();
55+
while p.unwrap().next.is_some() {
56+
len += 1;
57+
p = p.unwrap().next.as_ref();
58+
}
59+
}
60+
let idx = len - n;
61+
{
62+
let mut p = dummy_head.as_mut();
63+
for _ in 0..(idx) {
64+
p = p.unwrap().next.as_mut();
65+
}
66+
let next = p.as_mut().unwrap().next.as_mut().unwrap().next.take();
67+
p.as_mut().unwrap().next = next;
68+
}
69+
dummy_head.unwrap().next
70+
}
71+
}
72+
73+
// helper function for test
74+
pub fn to_list(vec: Vec<i32>) -> Option<Box<ListNode>> {
75+
let mut current = None;
76+
for &v in vec.iter().rev() {
77+
let mut node = ListNode::new(v);
78+
node.next = current;
79+
current = Some(Box::new(node));
80+
}
81+
current
82+
}
83+
84+
// submission codes end
85+
86+
#[cfg(test)]
87+
mod tests {
88+
use super::*;
89+
90+
#[test]
91+
fn test_19() {
92+
assert_eq!(Solution::remove_nth_from_end(to_list(vec![1,2,3,4,5]), 2),
93+
to_list(vec![1,2,3,5]));
94+
assert_eq!(Solution::remove_nth_from_end(to_list(vec![1]), 1),
95+
None);
96+
}
97+
}

src/n0020_valid_parentheses.rs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* [20] Valid Parentheses
3+
*
4+
* Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
5+
*
6+
* An input string is valid if:
7+
*
8+
* <ol>
9+
* Open brackets must be closed by the same type of brackets.
10+
* Open brackets must be closed in the correct order.
11+
* </ol>
12+
*
13+
* Note that an empty string is also considered valid.
14+
*
15+
* Example 1:
16+
*
17+
*
18+
* Input: "()"
19+
* Output: true
20+
*
21+
*
22+
* Example 2:
23+
*
24+
*
25+
* Input: "()[]{}"
26+
* Output: true
27+
*
28+
*
29+
* Example 3:
30+
*
31+
*
32+
* Input: "(]"
33+
* Output: false
34+
*
35+
*
36+
* Example 4:
37+
*
38+
*
39+
* Input: "([)]"
40+
* Output: false
41+
*
42+
*
43+
* Example 5:
44+
*
45+
*
46+
* Input: "{[]}"
47+
* Output: true
48+
*
49+
*
50+
*/
51+
pub struct Solution {}
52+
53+
// submission codes start here
54+
55+
impl Solution {
56+
pub fn is_valid(s: String) -> bool {
57+
let mut stack: Vec<char> = Vec::new();
58+
for ch in s.chars().into_iter() {
59+
match stack.last() {
60+
None => {},
61+
Some(&last) => {
62+
if Solution::pair(last, ch) {
63+
stack.pop();
64+
continue
65+
}
66+
},
67+
}
68+
stack.push(ch);
69+
}
70+
stack.is_empty()
71+
}
72+
73+
#[inline(always)]
74+
fn pair(open: char, close: char) -> bool {
75+
(open == '{' && close == '}') ||
76+
(open == '(' && close == ')') ||
77+
(open == '[' && close == ']')
78+
}
79+
}
80+
81+
// submission codes end
82+
83+
#[cfg(test)]
84+
mod tests {
85+
use super::*;
86+
87+
#[test]
88+
fn test_20() {
89+
assert_eq!(Solution::is_valid("()[]{}".to_string()), true);
90+
}
91+
}

0 commit comments

Comments
 (0)