Merge branch 'youngyangyang04:master' into master

This commit is contained in:
roylx
2022-11-15 10:18:21 -07:00
committed by GitHub
3 changed files with 91 additions and 0 deletions

View File

@ -493,6 +493,33 @@ object Solution {
}
```
rust:
```rust
impl Solution {
pub fn is_valid(s: String) -> bool {
if s.len() % 2 == 1 {
return false;
}
let mut stack = vec![];
let mut chars: Vec<char> = s.chars().collect();
while let Some(s) = chars.pop() {
match s {
')' => stack.push('('),
']' => stack.push('['),
'}' => stack.push('{'),
_ => {
if stack.is_empty() || stack.pop().unwrap() != s {
return false;
}
}
}
}
stack.is_empty()
}
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>

View File

@ -420,6 +420,41 @@ object Solution {
}
```
rust:
```rust
impl Solution {
pub fn eval_rpn(tokens: Vec<String>) -> i32 {
let mut stack = vec![];
for token in tokens.into_iter() {
match token.as_str() {
"+" => {
let a = stack.pop().unwrap();
*stack.last_mut().unwrap() += a;
}
"-" => {
let a = stack.pop().unwrap();
*stack.last_mut().unwrap() -= a;
}
"*" => {
let a = stack.pop().unwrap();
*stack.last_mut().unwrap() *= a;
}
"/" => {
let a = stack.pop().unwrap();
*stack.last_mut().unwrap() /= a;
}
_ => {
stack.push(token.parse::<i32>().unwrap());
}
}
}
stack.pop().unwrap()
}
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>

View File

@ -802,6 +802,35 @@ class myDequeue{
}
```
rust:
```rust
impl Solution {
pub fn max_sliding_window(nums: Vec<i32>, k: i32) -> Vec<i32> {
let mut res = vec![];
let mut queue = VecDeque::with_capacity(k as usize);
for (i, &v) in nums.iter().enumerate() {
// 如果队列长度超过 k那么需要移除队首过期元素
if i - queue.front().unwrap_or(&0) == k as usize {
queue.pop_front();
}
while let Some(&index) = queue.back() {
if nums[index] >= v {
break;
}
// 如果队列第一个元素比当前元素小,那么就把队列第一个元素弹出
queue.pop_back();
}
queue.push_back(i);
if i >= k as usize - 1 {
res.push(nums[queue[0]]);
}
}
res
}
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>