-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
153 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# 第五章 栈与队列part02 | ||
今日内容: | ||
|
||
● 20. 有效的括号 | ||
● 1047. 删除字符串中的所有相邻重复项 | ||
● 150. 逆波兰表达式求值 | ||
|
||
详细布置 | ||
|
||
## 20. 有效的括号 | ||
|
||
讲完了栈实现队列,队列实现栈,接下来就是栈的经典应用了。 | ||
|
||
大家先自己思考一下 有哪些不匹配的场景,在看视频 我讲的都有哪些场景,落实到代码其实就容易很多了。 | ||
|
||
题目链接/文章讲解/视频讲解:https://programmercarl.com/0020.%E6%9C%89%E6%95%88%E7%9A%84%E6%8B%AC%E5%8F%B7.html | ||
|
||
## 1047. 删除字符串中的所有相邻重复项 | ||
|
||
栈的经典应用。 | ||
|
||
要知道栈为什么适合做这种类似于爱消除的操作,因为栈帮助我们记录了 遍历数组当前元素时候,前一个元素是什么。 | ||
|
||
题目链接/文章讲解/视频讲解:https://programmercarl.com/1047.%E5%88%A0%E9%99%A4%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%B8%AD%E7%9A%84%E6%89%80%E6%9C%89%E7%9B%B8%E9%82%BB%E9%87%8D%E5%A4%8D%E9%A1%B9.html | ||
## 150. 逆波兰表达式求值 | ||
|
||
本题不难,但第一次做的话,会很难想到,所以先看视频,了解思路再去做题 | ||
|
||
题目链接/文章讲解/视频讲解:https://programmercarl.com/0150.%E9%80%86%E6%B3%A2%E5%85%B0%E8%A1%A8%E8%BE%BE%E5%BC%8F%E6%B1%82%E5%80%BC.html | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# 1047. 删除字符串中的所有相邻重复项 | ||
## 题目描述 | ||
|
||
给出由小写字母组成的字符串 S,重复项删除操作会选择两个相邻且相同的字母,并删除它们。 | ||
|
||
在 S 上反复执行重复项删除操作,直到无法继续删除。 | ||
|
||
在完成所有重复项删除操作后返回最终的字符串。答案保证唯一。 | ||
|
||
## 解题思路 | ||
|
||
```rust | ||
struct Solution {} | ||
|
||
impl Solution { | ||
pub fn remove_duplicates(s: String) -> String { | ||
let mut v = vec![]; | ||
for c in s.chars() { | ||
if let Some(&x) = v.last() { | ||
if x == c { v.pop(); } | ||
else { v.push(c) } | ||
} else { v.push(c) } | ||
} | ||
v.iter().collect() | ||
} | ||
} | ||
``` | ||
## 学习感想 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# 150. 逆波兰表达式求值 | ||
|
||
## 题目描述 | ||
|
||
给你一个字符串数组 tokens ,表示一个根据 逆波兰表示法 表示的算术表达式。 | ||
|
||
请你计算该表达式。返回一个表示表达式值的整数。 | ||
|
||
注意: | ||
|
||
有效的算符为 '+'、'-'、'*' 和 '/' 。 | ||
每个操作数(运算对象)都可以是一个整数或者另一个表达式。 | ||
两个整数之间的除法总是 向零截断 。 | ||
表达式中不含除零运算。 | ||
输入是一个根据逆波兰表示法表示的算术表达式。 | ||
答案及所有中间计算结果可以用 32 位 整数表示 | ||
|
||
## 解题思路 | ||
|
||
```rust | ||
struct Solution {} | ||
impl Solution { | ||
pub fn eval_rpn(tokens: Vec<String>) -> i32 { | ||
let mut v = vec![]; | ||
for s in tokens { | ||
let a = vec!["+","-","*","/"]; | ||
if a.contains(&&s[..]) { | ||
let x = v.pop().unwrap(); | ||
let y = v.pop().unwrap(); | ||
match &s[..] { | ||
"+" => {v.push(x+y)}, | ||
"-" => {v.push(y-x)}, | ||
"*" => {v.push(y*x)}, | ||
"/" => {v.push(y/x)}, | ||
_ => {}, | ||
} | ||
} else { | ||
v.push(s.parse::<i32>().unwrap()); | ||
} | ||
} | ||
v.pop().unwrap() | ||
} | ||
} | ||
``` | ||
|
||
## 学习感想 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# 20. 有效的括号 | ||
|
||
## 题目描述 | ||
|
||
给定一个只包括 '(',')','{','}','[',']' 的字符串 s ,判断字符串是否有效。 | ||
|
||
有效字符串需满足: | ||
|
||
左括号必须用相同类型的右括号闭合。 | ||
左括号必须以正确的顺序闭合。 | ||
每个右括号都有一个对应的相同类型的左括号。 | ||
|
||
|
||
## 解题思路 | ||
|
||
```rust | ||
struct Solution {} | ||
|
||
impl Solution { | ||
pub fn is_valid(s: String) -> bool { | ||
let mut v = vec![]; // only }]) | ||
for c in s.chars() { | ||
if c == '(' || c == '[' || c == '{' { | ||
v.push(Self::mutate(c)); | ||
} else { | ||
if let Some(&x) = v.last() { | ||
if x == c { v.pop(); } | ||
else { return false } | ||
} else { | ||
return false | ||
} | ||
} | ||
} | ||
v.is_empty() | ||
} | ||
fn mutate(x: char) -> char { | ||
if x == '(' { return ')' } | ||
else if x == '[' { return ']' } | ||
else {return '}'} | ||
} | ||
} | ||
``` | ||
|
||
## 学习感想 |