规范格式

This commit is contained in:
YDZ
2020-08-07 15:50:06 +08:00
parent 854a339abc
commit 4e11f4028a
1438 changed files with 907 additions and 924 deletions

View File

@ -0,0 +1,39 @@
package leetcode
import "fmt"
func largestRectangleArea(heights []int) int {
maxArea, stack, height := 0, []int{}, 0
for i := 0; i <= len(heights); i++ {
if i == len(heights) {
height = 0
} else {
height = heights[i]
}
if len(stack) == 0 || height >= heights[stack[len(stack)-1]] {
stack = append(stack, i)
} else {
tmp := stack[len(stack)-1]
fmt.Printf("1. tmp = %v stack = %v\n", tmp, stack)
stack = stack[:len(stack)-1]
length := 0
if len(stack) == 0 {
length = i
} else {
length = i - 1 - stack[len(stack)-1]
fmt.Printf("2. length = %v stack = %v i = %v\n", length, stack, i)
}
maxArea = max(maxArea, heights[tmp]*length)
fmt.Printf("3. maxArea = %v heights[tmp]*length = %v\n", maxArea, heights[tmp]*length)
i--
}
}
return maxArea
}
func max(a int, b int) int {
if a > b {
return a
}
return b
}

View File

@ -0,0 +1,52 @@
package leetcode
import (
"fmt"
"testing"
)
type question84 struct {
para84
ans84
}
// para 是参数
// one 代表第一个参数
type para84 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans84 struct {
one int
}
func Test_Problem84(t *testing.T) {
qs := []question84{
question84{
para84{[]int{2, 1, 5, 6, 2, 3}},
ans84{10},
},
question84{
para84{[]int{1}},
ans84{1},
},
question84{
para84{[]int{1, 1}},
ans84{2},
},
}
fmt.Printf("------------------------Leetcode Problem 84------------------------\n")
for _, q := range qs {
_, p := q.ans84, q.para84
fmt.Printf("【input】:%v 【output】:%v\n", p, largestRectangleArea(p.one))
}
fmt.Printf("\n\n\n")
}

View File

@ -0,0 +1,34 @@
# [84. Largest Rectangle in Histogram](https://leetcode.com/problems/largest-rectangle-in-histogram/)
## 题目
Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.
![](https://assets.leetcode.com/uploads/2018/10/12/histogram.png)
Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3].
![](https://assets.leetcode.com/uploads/2018/10/12/histogram_area.png)
The largest rectangle is shown in the shaded area, which has area = 10 unit.
Example:
```c
Input: [2,1,5,6,2,3]
Output: 10
```
## 题目大意
给出每个直方图的高度,要求在这些直方图之中找到面积最大的矩形,输出矩形的面积。
## 解题思路
用单调栈依次保存直方图的高度下标,一旦出现高度比栈顶元素小的情况就取出栈顶元素,单独计算一下这个栈顶元素的矩形的高度。然后停在这里(外层循环中的 i--,再 ++,就相当于停在这里了),继续取出当前最大栈顶的前一个元素,即连续弹出 2 个最大的,以稍小的一个作为矩形的边,宽就是 2 计算面积…………如果停在这里的下标代表的高度一直比栈里面的元素小,就一直弹出,取出最后一个比当前下标大的高度作为矩形的边。宽就是最后一个比当前下标大的高度和当前下标 i 的差值。计算出面积以后不断的更新 maxArea 即可。