Merge branch 'youngyangyang04:master' into master

This commit is contained in:
wang2jun
2022-11-20 22:34:56 +08:00
committed by GitHub
13 changed files with 421 additions and 12 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

@ -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++) {
@ -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;

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

@ -1018,7 +1018,7 @@ class MyStack {
}
}
```
>
>
```php
class MyStack {
public $queue;
@ -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"/>

View File

@ -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"/>

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"/>

View File

@ -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 一定是谁多了字符或者谁少了字符。

View File

@ -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">

View File

@ -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"/>

View File

@ -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"/>

View File

@ -666,6 +666,83 @@ object Solution {
}
}
```
rust:
```rust
impl Solution{
// 前序
pub fn preorder_traversal(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
let mut res = vec![];
let mut stack = vec![];
if root.is_some(){
stack.push(root);
}
while !stack.is_empty(){
if let Some(node) = stack.pop().unwrap(){
if node.borrow().right.is_some(){
stack.push(node.borrow().right.clone());
}
if node.borrow().left.is_some(){
stack.push(node.borrow().left.clone());
}
stack.push(Some(node));
stack.push(None);
}else{
res.push(stack.pop().unwrap().unwrap().borrow().val);
}
}
res
}
// 中序
pub fn inorder_traversal(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
let mut res = vec![];
let mut stack = vec![];
if root.is_some() {
stack.push(root);
}
while !stack.is_empty() {
if let Some(node) = stack.pop().unwrap() {
if node.borrow().right.is_some() {
stack.push(node.borrow().right.clone());
}
stack.push(Some(node.clone()));
stack.push(None);
if node.borrow().left.is_some() {
stack.push(node.borrow().left.clone());
}
} else {
res.push(stack.pop().unwrap().unwrap().borrow().val);
}
}
res
}
// 后序
pub fn postorder_traversal(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
let mut res = vec![];
let mut stack = vec![];
if root.is_some() {
stack.push(root);
}
while !stack.is_empty() {
if let Some(node) = stack.pop().unwrap() {
stack.push(Some(node.clone()));
stack.push(None);
if node.borrow().right.is_some() {
stack.push(node.borrow().right.clone());
}
if node.borrow().left.is_some() {
stack.push(node.borrow().left.clone());
}
} else {
res.push(stack.pop().unwrap().unwrap().borrow().val);
}
}
res
}
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>

View File

@ -640,6 +640,60 @@ object Solution {
}
}
```
rust:
```rust
use std::cell::RefCell;
use std::rc::Rc;
impl Solution {
//前序
pub fn preorder_traversal(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
let mut res = vec![];
let mut stack = vec![root];
while !stack.is_empty() {
if let Some(node) = stack.pop().unwrap() {
res.push(node.borrow().val);
stack.push(node.borrow().right.clone());
stack.push(node.borrow().left.clone());
}
}
res
}
//中序
pub fn inorder_traversal(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
let mut res = vec![];
let mut stack = vec![];
let mut node = root;
while !stack.is_empty() || node.is_some() {
while let Some(n) = node {
node = n.borrow().left.clone();
stack.push(n);
}
if let Some(n) = stack.pop() {
res.push(n.borrow().val);
node = n.borrow().right.clone();
}
}
res
}
//后序
pub fn postorder_traversal(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
let mut res = vec![];
let mut stack = vec![root];
while !stack.is_empty() {
if let Some(node) = stack.pop().unwrap() {
res.push(node.borrow().val);
stack.push(node.borrow().left.clone());
stack.push(node.borrow().right.clone());
}
}
res.into_iter().rev().collect()
}
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>

View File

@ -525,6 +525,46 @@ object Solution {
}
}
```
rust:
```rust
use std::cell::RefCell;
use std::rc::Rc;
impl Solution {
pub fn preorder_traversal(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
let mut res = vec![];
Self::traverse(&root, &mut res);
res
}
//前序遍历
pub fn traverse(root: &Option<Rc<RefCell<TreeNode>>>, res: &mut Vec<i32>) {
if let Some(node) = root {
res.push(node.borrow().val);
Self::traverse(&node.borrow().left, res);
Self::traverse(&node.borrow().right, res);
}
}
//后序遍历
pub fn traverse(root: &Option<Rc<RefCell<TreeNode>>>, res: &mut Vec<i32>) {
if let Some(node) = root {
Self::traverse(&node.borrow().left, res);
Self::traverse(&node.borrow().right, res);
res.push(node.borrow().val);
}
}
//中序遍历
pub fn traverse(root: &Option<Rc<RefCell<TreeNode>>>, res: &mut Vec<i32>) {
if let Some(node) = root {
Self::traverse(&node.borrow().left, res);
res.push(node.borrow().val);
Self::traverse(&node.borrow().right, res);
}
}
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>