mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 11:34:46 +08:00
Merge branch 'youngyangyang04:master' into master
This commit is contained in:
@ -206,7 +206,7 @@ class Solution {
|
|||||||
public int largestRectangleArea(int[] heights) {
|
public int largestRectangleArea(int[] heights) {
|
||||||
int length = heights.length;
|
int length = heights.length;
|
||||||
int[] minLeftIndex = new int [length];
|
int[] minLeftIndex = new int [length];
|
||||||
int[] maxRigthIndex = new int [length];
|
int[] minRightIndex = new int [length];
|
||||||
// 记录左边第一个小于该柱子的下标
|
// 记录左边第一个小于该柱子的下标
|
||||||
minLeftIndex[0] = -1 ;
|
minLeftIndex[0] = -1 ;
|
||||||
for (int i = 1; i < length; i++) {
|
for (int i = 1; i < length; i++) {
|
||||||
@ -215,17 +215,17 @@ class Solution {
|
|||||||
while (t >= 0 && heights[t] >= heights[i]) t = minLeftIndex[t];
|
while (t >= 0 && heights[t] >= heights[i]) t = minLeftIndex[t];
|
||||||
minLeftIndex[i] = t;
|
minLeftIndex[i] = t;
|
||||||
}
|
}
|
||||||
// 记录每个柱子 右边第一个小于该柱子的下标
|
// 记录每个柱子右边第一个小于该柱子的下标
|
||||||
maxRigthIndex[length - 1] = length;
|
minRightIndex[length - 1] = length;
|
||||||
for (int i = length - 2; i >= 0; i--) {
|
for (int i = length - 2; i >= 0; i--) {
|
||||||
int t = i + 1;
|
int t = i + 1;
|
||||||
while(t < length && heights[t] >= heights[i]) t = maxRigthIndex[t];
|
while(t < length && heights[t] >= heights[i]) t = minRightIndex[t];
|
||||||
maxRigthIndex[i] = t;
|
minRightIndex[i] = t;
|
||||||
}
|
}
|
||||||
// 求和
|
// 求和
|
||||||
int result = 0;
|
int result = 0;
|
||||||
for (int i = 0; i < length; i++) {
|
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);
|
result = Math.max(sum, result);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
@ -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">
|
<p align="center">
|
||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||||
|
Reference in New Issue
Block a user