Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from youngyangyang04:master #352

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 4 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Sep 28, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
修改 0084.柱状图中最大的矩形 Go单调栈代码的格式
  • Loading branch information
ShuangmingMa authored Sep 16, 2023
commit c26fddc702a333a3ed36fa10ebbf0f71d617ce7b
56 changes: 27 additions & 29 deletions problems/0084.柱状图中最大的矩形.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -478,36 +478,34 @@ class Solution:

```go
func largestRectangleArea(heights []int) int {
// 声明max并初始化为0
max := 0
// 使用切片实现栈
stack := make([]int, 0)
// 数组头部加入0
heights = append([]int{0}, heights...)
// 数组尾部加入0
heights = append(heights, 0)
// 初始化栈,序号从0开始
stack = append(stack, 0)
for i := 1; i < len(heights); i++ {
// 结束循环条件为:当即将入栈元素>top元素,也就是形成非单调递增的趋势
for heights[stack[len(stack)-1]] > heights[i] {
// mid 是top
mid := stack[len(stack)-1]
// 出栈
stack = stack[0 : len(stack)-1]
// left是top的下一位元素,i是将要入栈的元素
left := stack[len(stack)-1]
// 高度x宽度
tmp := heights[mid] * (i - left - 1)
if tmp > max {
max = tmp
}
}
stack = append(stack, i)
}
return max
max := 0
// 使用切片实现栈
stack := make([]int, 0)
// 数组头部加入0
heights = append([]int{0}, heights...)
// 数组尾部加入0
heights = append(heights, 0)
// 初始化栈,序号从0开始
stack = append(stack, 0)
for i := 1; i < len(heights); i ++ {
// 结束循环条件为:当即将入栈元素>top元素,也就是形成非单调递增的趋势
for heights[stack[len(stack) - 1]] > heights[i] {
// mid 是top
mid := stack[len(stack) - 1]
// 出栈
stack = stack[0 : len(stack) - 1]
// left是top的下一位元素,i是将要入栈的元素
left := stack[len(stack) - 1]
// 高度x宽度
tmp := heights[mid] * (i - left - 1)
if tmp > max {
max = tmp
}
}
stack = append(stack, i)
}
return max
}

```

### JavaScript:
Expand Down

AltStyle によって変換されたページ (->オリジナル) /