Skip to content

Commit af78d57

Browse files
committed
feat: add more
1 parent cb1bf4b commit af78d57

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

Array/_1629_slowest_key.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub fn slowest_key(release_times: Vec<i32>, keys_pressed: String) -> char {
5151
let chars = keys_pressed.chars().collect::<Vec<char>>();
5252
let mut max_char = chars[0];
5353
for i in 1..chars.len() {
54-
let diff = (release_times[i] - release_times[i - 1]);
54+
let diff = release_times[i] - release_times[i - 1];
5555
if diff > max_time || (diff == max_time && chars[i] > max_char) {
5656
max_time = diff;
5757
max_char = chars[i];

stack/_0020_valid_parentheses.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// https://leetcode.com/problems/valid-parentheses
2+
//
3+
// Given a string `s` containing just the characters `'('`, `')'`, `'{'`, `'}'`, `'['` and `']'`, determine if the input string is valid.
4+
//
5+
// An input string is valid if:
6+
//
7+
// 1. Open brackets must be closed by the same type of brackets.
8+
// 2. Open brackets must be closed in the correct order.
9+
//
10+
// **Example 1:**
11+
//
12+
// ```
13+
// **Input:** s = "()"
14+
// **Output:** true
15+
// ```
16+
//
17+
// **Example 2:**
18+
//
19+
// ```
20+
// **Input:** s = "()[]{}"
21+
// **Output:** true
22+
// ```
23+
//
24+
// **Example 3:**
25+
//
26+
// ```
27+
// **Input:** s = "(]"
28+
// **Output:** false
29+
// ```
30+
//
31+
// **Constraints:**
32+
//
33+
// * `1 <= s.length <= 10<sup>4</sup>`
34+
// * `s` consists of parentheses only `'()[]{}'`.
35+
36+
pub fn is_valid(s: String) -> bool {
37+
let mut stack = Vec::new();
38+
for c in s.chars() {
39+
match c {
40+
'(' => stack.push('('),
41+
'{' => stack.push('{'),
42+
'[' => stack.push('['),
43+
')' => {
44+
if stack.pop() != Some('(') {
45+
return false;
46+
}
47+
}
48+
'}' => {
49+
if stack.pop() != Some('{') {
50+
return false;
51+
}
52+
}
53+
']' => {
54+
if stack.pop() != Some('[') {
55+
return false;
56+
}
57+
}
58+
_ => {}
59+
}
60+
}
61+
return stack.is_empty();
62+
}
63+
64+
#[test]
65+
pub fn t1() {
66+
assert_eq!(is_valid("()".to_string()), true);
67+
}

stack/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
#![allow(dead_code)]
22

3+
mod _0020_valid_parentheses;
34
mod _1598_crawler_log_folder;

0 commit comments

Comments
 (0)