文章目录
- 栈
- https://leetcode-cn.com/problems/count-binary-substrings/
- https://leetcode-cn.com/problems/build-an-array-with-stack-operations/
- https://leetcode-cn.com/problems/min-stack/
- https://leetcode-cn.com/problems/find-mode-in-binary-search-tree/
- https://leetcode-cn.com/problems/design-a-stack-with-increment-operation/
- https://leetcode-cn.com/problems/validate-stack-sequences/submissions/
- 计算器类比较简单
- https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/submissions/
- https://leetcode-cn.com/problems/trapping-rain-water/submissions/
栈
type Stack struct {
top *node
length int
}
type node struct {
value interface{}
prev *node
}
// 创建一个栈
func New() *Stack {
return &Stack{nil, 0}
}
// 取栈长度
func (s *Stack) Length() int {
return s.length
}
// 查看栈顶元素
func (s *Stack) Peek() interface{} {
if s.length == 0 {
return nil
}
return s.top.value
}
// 出栈
func (s *Stack) Pop() interface{} {
if s.length == 0 {
return nil
}
n := s.top
s.top = n.prev
s.length--
return n.value
}
// 入栈
func (s *Stack) Push(value interface{}) {
n := &node{value, s.top}
s.top = n
s.length++
}
https://leetcode-cn.com/problems/count-binary-substrings/
找:
这道题找的是相邻的两组0/1的高度里的最小值。
如何在O(n)内获取每组0/1的高度呢?
那么我这里首先想到,和上一个字符串比较,并且压栈。当字符串不相等时,取栈高度。建立新栈,入栈。 循环体依赖于一个临时字符空间c来保留上一个字符0/1和一个临时int空间last来保留上一次1/0的栈高度。 最后把留在栈里的最后一组字符加上去。
使用链表栈,处于性能考虑。
增: N/A
删: N/A
Code
func countBinarySubstrings(s string) int {
ans := 0
last := 0
stack := &Stack{}
counts := []int{}
c := s[0]
for i , _ := range s {
if c == s[i] {
stack.Push(1)
} else {
ans += min(last,stack.Length())
counts = append(counts, stack.Length())
last = stack.Length()
stack = &Stack{}
stack.Push(1)
c = s[i]
}
}
ans += min(last,stack.Length())
return ans
}
https://leetcode-cn.com/problems/build-an-array-with-stack-operations/
找
for n = 1 … target
增
n = target n
删
n != target n
Code
func buildArray(target []int, n int) []string {
operators := []string{}
flag := 0
for i :=1; i<= n; i++ {
operators = append(operators,"Push")
if i == target[flag]{
flag++
if flag == len(target) {
return operators
}
} else {
operators = append(operators,"Pop")
}
}
return operators
}
https://leetcode-cn.com/problems/min-stack/
找
找最小? 插入时判断即可?
增
增加元素本身,在另外一个栈上增加最小值
删
code
type MinStack struct {
stack *Stack
minStack *Stack
}
/** initialize your data structure here. */
func Constructor() MinStack {
return MinStack{
stack: &Stack{},
minStack: &Stack{},
}
}
func (this *MinStack) Push(x int) {
this.stack.Push(x)
var top int
if this.minStack.Length() == 0 {
top = x
} else {
top = this.minStack.Peek().(int)
}
top = min(top,x)
this.minStack.Push(top)
}
func (this *MinStack) Pop() {
this.stack.Pop()
this.minStack.Pop()
}
func (this *MinStack) Top() int {
return this.stack.Peek().(int)
}
func (this *MinStack) GetMin() int {
return this.minStack.Peek().(int)
}
https://leetcode-cn.com/problems/find-mode-in-binary-search-tree/
找
左子=当前的值入栈,求最高栈高度
增
删
Code
var max int //最大值
var res []int //结果
var cur int //当前
var counter int //当前计数
func findMode(root *TreeNode) []int {
res, max, cur, counter = []int{}, 1, 0, 0
dfs(root)
return res
}
func dfs(root *TreeNode) {
if root != nil {
dfs(root.Left)
if root.Val != cur {
counter = 0
}
counter++
if max < counter {
max = counter
res = []int{root.Val}
} else if max == counter {
res = append(res, root.Val)
}
cur = root.Val
dfs(root.Right)
}
}
https://leetcode-cn.com/problems/design-a-stack-with-increment-operation/
找
如题
增
如题
删
如题
Code
type CustomStack struct {
length int
max int
space []int
}
func Constructor(maxSize int) CustomStack {
return CustomStack{
length : 0,
max : maxSize,
space : make([]int,maxSize),
}
}
func (this *CustomStack) Push(x int) {
if this.length == this.max {
return
}
this.space[this.length] = x
this.length++
}
func (this *CustomStack) Pop() int {
if this.length == 0 {
return -1
}
i:= this.space[this.length-1]
this.length--
return i
}
func (this *CustomStack) Increment(k int, val int) {
turn := 0
if k< this.max {
turn = k
} else {
turn = this.max
}
for i:=0; i< turn; i++ {
this.space[i] += val
}
}
/**
* Your CustomStack object will be instantiated and called as such:
* obj := Constructor(maxSize);
* obj.Push(x);
* param_2 := obj.Pop();
* obj.Increment(k,val);
*/
https://leetcode-cn.com/problems/validate-stack-sequences/submissions/
两个栈, 一个倒叙栈,一个正序栈
找
找一致
增
不一致,正序栈++
删
如果一致倒叙栈–, 正序栈–
Code
func validateStackSequences(pushed []int, popped []int) bool {
pushstack := &Stack{}
popstack := &Stack{}
for k := len(popped)-1; k>-1;k-- {
popstack.Push(popped[k])
}
for _, i := range pushed {
if i != popstack.Peek().(int) {
pushstack.Push(i)
} else {
popstack.Pop()
for {
if pushstack.Peek() == nil || pushstack.Peek().(int) != popstack.Peek().(int) {
break;
}
pushstack.Pop()
popstack.Pop()
}
}
}
return pushstack.Length() == 0
}
计算器类比较简单
https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/submissions/
找准入栈,出栈时机即可
Code
func evalRPN(tokens []string) int {
stack := &Stack{}
for _, str := range tokens {
if str == "+" {
t := stack.Pop().(int)
i := stack.Pop().(int)
fmt.Println(t)
fmt.Println(i)
s := t + i
fmt.Println(s)
stack.Push(s)
}
if str == "-" {
t := stack.Pop().(int)
i := stack.Pop().(int)
s := i - t
stack.Push(s)
}
if str == "*" {
t := stack.Pop().(int)
i := stack.Pop().(int)
fmt.Println(t)
fmt.Println(i)
s := t * i
fmt.Println(s)
stack.Push(s)
}
if str == "/" {
t := stack.Pop().(int)
i := stack.Pop().(int)
fmt.Println(t)
fmt.Println(i)
s := i /t
fmt.Println(s)
stack.Push(s)
}
i, err := strconv.Atoi(str)
if err == nil {
fmt.Println(i)
stack.Push(i)
}
}
return stack.Pop().(int)
}
https://leetcode-cn.com/problems/trapping-rain-water/submissions/
找
我们需要找的是左右高度和当前高度的差值
所以栈A当前栈, 栈B当前最高栈(也就是左侧高度)
入栈条件,非空/补位
增
删
Code
func trap(height []int) int {
count :=0;
stack := &Stack{}
hight_stack := &Stack{}
for _, current := range height {
if current == 0 && stack.Length() == 0 {
continue
}// pass 0s before 1st block
k := 1
for stack.Length()>0 && current > stack.Peek().(int) {
count += min(hight_stack.Peek().(int),current) - stack.Peek().(int)
hight_stack.Pop()
stack.Pop()
k++ // 补位
}
hi := current
for i:=0; i<k; i++ {
if hight_stack.Peek() != nil {
hi = max(current,hight_stack.Peek().(int))
}
hight_stack.Push(hi)
stack.Push(current)
}
}
return count
}