This commit is contained in:
2026-07-17 14:39:23 +08:00
parent 4b12dd8f07
commit 3bd18ddaf2
3 changed files with 165 additions and 0 deletions
+43
View File
@@ -0,0 +1,43 @@
package top100liked
import (
"fmt"
"strings"
"testing"
"unicode"
)
// https://leetcode.cn/problems/decode-string/description/?envType=study-plan-v2&envId=top-100-liked
func decodeString(s string) string {
curStr := ""
curNum := 0
strStack := []string{}
numStack := []int{}
for _, r := range s {
if unicode.IsDigit(r) {
curNum = curNum*10 + int(r-'0')
} else if unicode.IsLetter(r) {
curStr += string(r)
} else if r == '[' {
strStack = append(strStack, curStr)
curStr = ""
numStack = append(numStack, curNum)
curNum = 0
} else if r == ']' {
num := numStack[len(numStack)-1]
prevStr := strStack[len(strStack)-1]
numStack = numStack[:len(numStack)-1]
strStack = strStack[:len(strStack)-1]
curStr = prevStr + strings.Repeat(curStr, num)
}
}
return curStr
}
func Test(t *testing.T) {
fmt.Println(decodeString("3[a]2[bc]"))
fmt.Println(decodeString("3[a2[c]]"))
fmt.Println(decodeString("2[abc]3[cd]ef"))
fmt.Println(decodeString("abc3[cd]xyz"))
}
+62
View File
@@ -0,0 +1,62 @@
package top100liked
import (
"fmt"
"testing"
)
// https://leetcode.cn/problems/daily-temperatures/?envType=study-plan-v2&envId=top-100-liked
func dailyTemperatures(temperatures []int) []int {
res := make([]int, len(temperatures))
stack := make([]int, 0, len(temperatures))
for i := len(temperatures) - 1; i >= 0; i-- {
for len(stack) > 0 && temperatures[stack[len(stack)-1]] <= temperatures[i] {
stack = stack[:len(stack)-1]
}
if len(stack) > 0 {
res[i] = stack[len(stack)-1] - i
}
stack = append(stack, i)
}
return res
}
// func dailyTemperatures(temperatures []int) []int {
// type item struct {
// Idx int
// Temp int
// }
// stack := []item{}
// res := make([]int, len(temperatures))
// for i := len(temperatures) - 1; i >= 0; i-- {
// cur := temperatures[i]
// if len(stack) == 0 {
// stack = append(stack, item{i, cur})
// res[i] = 0
// } else {
// for {
// if len(stack) == 0 {
// stack = append(stack, item{i, cur})
// res[i] = 0
// break
// }
// top := stack[len(stack)-1]
// if cur < top.Temp {
// res[i] = top.Idx - i
// stack = append(stack, item{i, cur})
// break
// } else {
// stack = stack[:len(stack)-1] // pop
// }
// }
// }
// }
// return res
// }
func Test(t *testing.T) {
fmt.Printf("%+v", dailyTemperatures([]int{73, 74, 75, 71, 69, 72, 76, 73}))
}
+60
View File
@@ -0,0 +1,60 @@
package top100liked
import (
"fmt"
"testing"
)
// https://leetcode.cn/problems/largest-rectangle-in-histogram/?envType=study-plan-v2&envId=top-100-liked
// 暴力法,超时
// func largestRectangleArea(heights []int) int {
// maxArea := 0
// // i 是每个组合中的元素数量
// for i := 1; i <= len(heights); i++ {
// // j 是在整个 heights 中滑动的次数
// for j := 0; j < len(heights)-i+1; j++ {
// // 计算
// maxArea = max(maxArea, slices.Min(heights[j:j+i])*i)
// }
// }
// return maxArea
// }
func largestRectangleArea(heights []int) int {
stack := []int{}
maxArea := 0
for i := range len(heights) + 1 {
if len(stack) == 0 {
stack = append(stack, i)
continue
}
var cur int
if i == len(heights) {
cur = 0
} else {
cur = heights[i]
}
if cur >= heights[stack[len(stack)-1]] {
stack = append(stack, i)
} else {
for len(stack) > 0 && heights[stack[len(stack)-1]] > cur {
top := stack[len(stack)-1]
stack = stack[:len(stack)-1]
width := i
if len(stack) > 0 {
width = i - stack[len(stack)-1] - 1
}
maxArea = max(maxArea, heights[top]*width)
}
stack = append(stack, i)
}
}
return maxArea
}
func Test(t *testing.T) {
fmt.Println(largestRectangleArea([]int{2, 1, 5, 6, 2, 3}))
fmt.Println(largestRectangleArea([]int{1}))
fmt.Println(largestRectangleArea([]int{2, 1, 2}))
}