Skip to content

Commit

Permalink
Merge branch 'youngyangyang04:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
wang2jun authored Nov 20, 2022
2 parents d5392f4 + c15b8a7 commit a6f2d48
Show file tree
Hide file tree
Showing 13 changed files with 421 additions and 12 deletions.
27 changes: 27 additions & 0 deletions problems/0020.有效的括号.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"/>
Expand Down
12 changes: 6 additions & 6 deletions problems/0084.柱状图中最大的矩形.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ class Solution {
public int largestRectangleArea(int[] heights) {
int length = heights.length;
int[] minLeftIndex = new int [length];
int[] maxRigthIndex = new int [length];
int[] minRightIndex = new int [length];
// 记录左边第一个小于该柱子的下标
minLeftIndex[0] = -1 ;
for (int i = 1; i < length; i++) {
Expand All @@ -215,17 +215,17 @@ class Solution {
while (t >= 0 && heights[t] >= heights[i]) t = minLeftIndex[t];
minLeftIndex[i] = t;
}
// 记录每个柱子 右边第一个小于该柱子的下标
maxRigthIndex[length - 1] = length;
// 记录每个柱子右边第一个小于该柱子的下标
minRightIndex[length - 1] = length;
for (int i = length - 2; i >= 0; i--) {
int t = i + 1;
while(t < length && heights[t] >= heights[i]) t = maxRigthIndex[t];
maxRigthIndex[i] = t;
while(t < length && heights[t] >= heights[i]) t = minRightIndex[t];
minRightIndex[i] = t;
}
// 求和
int result = 0;
for (int i = 0; i < length; i++) {
int sum = heights[i] * (maxRigthIndex[i] - minLeftIndex[i] - 1);
int sum = heights[i] * (minRightIndex[i] - minLeftIndex[i] - 1);
result = Math.max(sum, result);
}
return result;
Expand Down
35 changes: 35 additions & 0 deletions problems/0150.逆波兰表达式求值.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"/>
Expand Down
40 changes: 39 additions & 1 deletion problems/0225.用队列实现栈.md
Original file line number Diff line number Diff line change
Expand Up @@ -1018,7 +1018,7 @@ class MyStack {
}
}
```
> 单对列
> 单队列
```php
class MyStack {
public $queue;
Expand Down Expand Up @@ -1051,6 +1051,44 @@ class MyStack {
}
}
```

> rust:单队列
```rust
struct MyStack {
queue: Vec<i32>,
}

impl MyStack {
fn new() -> Self {
MyStack { queue: vec![] }
}

fn push(&mut self, x: i32) {
self.queue.push(x);
}

fn pop(&mut self) -> i32 {
let len = self.queue.len() - 1;
for _ in 0..len {
let tmp = self.queue.remove(0);
self.queue.push(tmp);
}
self.queue.remove(0)
}

fn top(&mut self) -> i32 {
let res = self.pop();
self.queue.push(res);
res
}

fn empty(&self) -> bool {
self.queue.is_empty()
}
}
```

<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
Expand Down
41 changes: 41 additions & 0 deletions problems/0232.用栈实现队列.md
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,47 @@ class MyQueue() {
}
```

rust:

```rust
struct MyQueue {
stack_in: Vec<i32>,
stack_out: Vec<i32>,
}
impl MyQueue {
fn new() -> Self {
MyQueue {
stack_in: Vec::new(),
stack_out: Vec::new(),
}

}

fn push(&mut self, x: i32) {
self.stack_in.push(x);
}

fn pop(&mut self) -> i32 {
if self.stack_out.is_empty(){
while !self.stack_in.is_empty() {
self.stack_out.push(self.stack_in.pop().unwrap());
}
}
self.stack_out.pop().unwrap()
}

fn peek(&mut self) -> i32 {
let res = self.pop();
self.stack_out.push(res);
res
}

fn empty(&self) -> bool {
self.stack_in.is_empty() && self.stack_out.is_empty()
}
}
```

<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
Expand Down
29 changes: 29 additions & 0 deletions problems/0239.滑动窗口最大值.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"/>
Expand Down
9 changes: 4 additions & 5 deletions problems/0242.有效的字母异位词.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,11 @@ Python:
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
record = [0] * 26
for i in range(len(s)):
for i in s:
#并不需要记住字符a的ASCII,只要求出一个相对数值就可以了
record[ord(s[i]) - ord("a")] += 1
print(record)
for i in range(len(t)):
record[ord(t[i]) - ord("a")] -= 1
record[ord(i) - ord("a")] += 1
for i in t:
record[ord(i) - ord("a")] -= 1
for i in range(26):
if record[i] != 0:
#record数组如果有的元素不为零0,说明字符串s和t 一定是谁多了字符或者谁少了字符。
Expand Down
27 changes: 27 additions & 0 deletions problems/0347.前K个高频元素.md
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,34 @@ object Solution {
.map(_._1)
}
}
```

rust: 小根堆

```rust
use std::cmp::Reverse;
use std::collections::{BinaryHeap, HashMap};
impl Solution {
pub fn top_k_frequent(nums: Vec<i32>, k: i32) -> Vec<i32> {
let mut hash = HashMap::new();
let mut heap = BinaryHeap::with_capacity(k as usize);
nums.into_iter().for_each(|k| {
*hash.entry(k).or_insert(0) += 1;
});

for (k, v) in hash {
if heap.len() == heap.capacity() {
if *heap.peek().unwrap() < (Reverse(v), k) {
continue;
} else {
heap.pop();
}
}
heap.push((Reverse(v), k));
}
heap.into_iter().map(|(_, k)| k).collect()
}
}
```

<p align="center">
Expand Down
19 changes: 19 additions & 0 deletions problems/1047.删除字符串中的所有相邻重复项.md
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,25 @@ object Solution {
}
```

rust:

```rust
impl Solution {
pub fn remove_duplicates(s: String) -> String {
let mut stack = vec![];
let mut chars: Vec<char> = s.chars().collect();
while let Some(c) = chars.pop() {
if stack.is_empty() || stack[stack.len() - 1] != c {
stack.push(c);
} else {
stack.pop();
}
}
stack.into_iter().rev().collect()
}
}
```

<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
Expand Down
23 changes: 23 additions & 0 deletions problems/二叉树理论基础.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,29 @@ class TreeNode(_value: Int = 0, _left: TreeNode = null, _right: TreeNode = null)
var right: TreeNode = _right
}
```

rust:

```rust
#[derive(Debug, PartialEq, Eq)]
pub struct TreeNode<T> {
pub val: T,
pub left: Option<Rc<RefCell<TreeNode<T>>>>,
pub right: Option<Rc<RefCell<TreeNode<T>>>>,
}

impl<T> TreeNode<T> {
#[inline]
pub fn new(val: T) -> Self {
TreeNode {
val,
left: None,
right: None,
}
}
}
```

<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
Expand Down
Loading

0 comments on commit a6f2d48

Please sign in to comment.