diff --git "a/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" "b/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" index 3d986a822c..69713c62fc 100644 --- "a/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" +++ "b/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" @@ -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 = 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() + } +} +``` +

diff --git "a/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" "b/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" index 220b096ea3..bcb0915e9c 100644 --- "a/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" +++ "b/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" @@ -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; diff --git "a/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" "b/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" index bf90b89b60..78dfae3ea1 100644 --- "a/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" +++ "b/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" @@ -420,6 +420,41 @@ object Solution { } ``` + +rust: + +```rust +impl Solution { + pub fn eval_rpn(tokens: Vec) -> 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::().unwrap()); + } + } + } + stack.pop().unwrap() + } +} +``` +

diff --git "a/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" "b/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" index 9aa8d8a119..2f01527270 100644 --- "a/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" +++ "b/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" @@ -1018,7 +1018,7 @@ class MyStack { } } ``` -> 单对列 +> 单队列 ```php class MyStack { public $queue; @@ -1051,6 +1051,44 @@ class MyStack { } } ``` + +> rust:单队列 + +```rust +struct MyStack { + queue: Vec, +} + +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() + } +} +``` +

diff --git "a/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" "b/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" index 1ed0c5d62b..5858f9e0e4 100644 --- "a/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" +++ "b/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" @@ -622,6 +622,47 @@ class MyQueue() { } ``` +rust: + +```rust +struct MyQueue { + stack_in: Vec, + stack_out: Vec, +} +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() + } +} +``` +

diff --git "a/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" "b/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" index 2d556d8b6d..40da879c3b 100644 --- "a/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" +++ "b/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" @@ -802,6 +802,35 @@ class myDequeue{ } ``` +rust: + +```rust +impl Solution { + pub fn max_sliding_window(nums: Vec, k: i32) -> Vec { + 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 + } +} +``` +

diff --git "a/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" "b/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" index f8ebc545b8..b7100b52de 100644 --- "a/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" +++ "b/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" @@ -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 一定是谁多了字符或者谁少了字符。 diff --git "a/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" "b/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" index 4745645538..56dbaa33b3 100644 --- "a/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" +++ "b/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" @@ -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, k: i32) -> Vec { + 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() + } +} ```

diff --git "a/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" "b/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" index 849b5e7904..ad729298d5 100644 --- "a/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" +++ "b/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" @@ -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 = 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() + } +} +``` +

diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" index e377626c8b..034229602a 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -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 { + pub val: T, + pub left: Option>>>, + pub right: Option>>>, +} + +impl TreeNode { + #[inline] + pub fn new(val: T) -> Self { + TreeNode { + val, + left: None, + right: None, + } + } +} +``` +

diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" index 030d3ae19e..69c23e5c6c 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" @@ -666,6 +666,83 @@ object Solution { } } ``` + +rust: + +```rust +impl Solution{ + // 前序 + pub fn preorder_traversal(root: Option>>) -> Vec { + 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>>) -> Vec { + 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>>) -> Vec { + 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 + } +} +``` +

diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" index a20f11cbab..2f67c323fa 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" @@ -640,6 +640,60 @@ object Solution { } } ``` + +rust: + +```rust +use std::cell::RefCell; +use std::rc::Rc; +impl Solution { + //前序 + pub fn preorder_traversal(root: Option>>) -> Vec { + 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>>) -> Vec { + 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>>) -> Vec { + 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() + } +} +``` +

diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" index 03304cafee..788610403b 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" @@ -525,6 +525,46 @@ object Solution { } } ``` + +rust: + +```rust +use std::cell::RefCell; +use std::rc::Rc; +impl Solution { + pub fn preorder_traversal(root: Option>>) -> Vec { + let mut res = vec![]; + Self::traverse(&root, &mut res); + res + } + +//前序遍历 + pub fn traverse(root: &Option>>, res: &mut Vec) { + 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>>, res: &mut Vec) { + 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>>, res: &mut Vec) { + if let Some(node) = root { + Self::traverse(&node.borrow().left, res); + res.push(node.borrow().val); + Self::traverse(&node.borrow().right, res); + } + } +} +``` +