# æ åéå
## ç®ä»
æ çç¹ç¹æ¯åå
¥å
åº

æ ¹æ®è¿ä¸ªç¹ç¹å¯ä»¥ä¸´æ¶ä¿åä¸äºæ°æ®ï¼ä¹åç¨å°ä¾æ¬¡åå¼¹åºæ¥ï¼å¸¸ç¨äº DFS 深度æç´¢
éåä¸è¬å¸¸ç¨äº BFS 广度æç´¢ï¼ç±»ä¼¼ä¸å±ä¸å±çæç´¢
## Stack æ
[min-stack](https://leetcode-cn.com/problems/min-stack/)
> 设计ä¸ä¸ªæ¯æ pushï¼popï¼top æä½ï¼å¹¶è½å¨å¸¸æ°æ¶é´å
æ£ç´¢å°æå°å
ç´ çæ ã
æè·¯ï¼ç¨ä¸¤ä¸ªæ å®ç°ï¼ä¸ä¸ªæå°æ å§ç»ä¿è¯æå°å¼å¨é¡¶é¨
```go
type MinStack struct {
min []int
stack []int
}
/** initialize your data structure here. */
func Constructor() MinStack {
return MinStack{
min: make([]int, 0),
stack: make([]int, 0),
}
}
func (this *MinStack) Push(x int) {
min := this.GetMin()
if x < min {
this.min = append(this.min, x)
} else {
this.min = append(this.min, min)
}
this.stack = append(this.stack, x)
}
func (this *MinStack) Pop() {
if len(this.stack) == 0 {
return
}
this.stack = this.stack[:len(this.stack)-1]
this.min = this.min[:len(this.min)-1]
}
func (this *MinStack) Top() int {
if len(this.stack) == 0 {
return 0
}
return this.stack[len(this.stack)-1]
}
func (this *MinStack) GetMin() int {
if len(this.min) == 0 {
return 1 << 31
}
min := this.min[len(this.min)-1]
return min
}
/**
* Your MinStack object will be instantiated and called as such:
* obj := Constructor();
* obj.Push(x);
* obj.Pop();
* param_3 := obj.Top();
* param_4 := obj.GetMin();
*/
```
[evaluate-reverse-polish-notation](https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/)
> **æ³¢å
°è¡¨è¾¾å¼è®¡ç®** > **è¾å
¥:** ["2", "1", "+", "3", "*"] > **è¾åº:** 9
> **è§£é:** ((2 + 1) \* 3) = 9
æè·¯ï¼éè¿æ ä¿å忥çå
ç´ ï¼éå°è¡¨è¾¾å¼å¼¹åºè¿ç®ï¼åæ¨å
¥ç»æï¼éå¤è¿ä¸ªè¿ç¨
```go
func evalRPN(tokens []string) int {
if len(tokens)==0{
return 0
}
stack:=make([]int,0)
for i:=0;i ç»å®ä¸ä¸ªç»è¿ç¼ç çå符串ï¼è¿åå®è§£ç åçå符串ã
> s = "3[a]2[bc]", è¿å "aaabcbc".
> s = "3[a2[c]]", è¿å "accaccacc".
> s = "2[abc]3[cd]ef", è¿å "abcabccdcdcdef".
æè·¯ï¼éè¿æ è¾
å©è¿è¡æä½
```go
func decodeString(s string) string {
if len(s) == 0 {
return ""
}
stack := make([]byte, 0)
for i := 0; i < len(s); i++ {
switch s[i] {
case ']':
temp := make([]byte, 0)
for len(stack) != 0 && stack[len(stack)-1] != '[' {
v := stack[len(stack)-1]
stack = stack[:len(stack)-1]
temp = append(temp, v)
}
// pop '['
stack = stack[:len(stack)-1]
// pop num
idx := 1
for len(stack) >= idx && stack[len(stack)-idx] >= '0' && stack[len(stack)-idx] <= '9' {
idx++
}
// 注æç´¢å¼è¾¹ç
num := stack[len(stack)-idx+1:]
stack = stack[:len(stack)-idx+1]
count, _ := strconv.Atoi(string(num))
for j := 0; j < count; j++ {
// æå符æ£åæ¾åå°æ éé¢
for j := len(temp) - 1; j >= 0; j-- {
stack = append(stack, temp[j])
}
}
default:
stack = append(stack, s[i])
}
}
return string(stack)
}
```
å©ç¨æ è¿è¡ DFS éå½æç´¢æ¨¡æ¿
```go
boolean DFS(int root, int target) {
Set visited;
Stack s;
add root to s;
while (s is not empty) {
Node cur = the top element in s;
return true if cur is target;
for (Node next : the neighbors of cur) {
if (next is not in visited) {
add next to s;
add next to visited;
}
}
remove cur from s;
}
return false;
}
```
[binary-tree-inorder-traversal](https://leetcode-cn.com/problems/binary-tree-inorder-traversal/)
> ç»å®ä¸ä¸ªäºåæ ï¼è¿åå®ç*ä¸åº*éåã
```go
// æè·¯ï¼éè¿stack ä¿åå·²ç»è®¿é®çå
ç´ ï¼ç¨äºåè·¯è¿å
func inorderTraversal(root *TreeNode) []int {
result := make([]int, 0)
if root == nil {
return result
}
stack := make([]*TreeNode, 0)
for len(stack) > 0 || root != nil {
for root != nil {
stack = append(stack, root)
root = root.Left // ä¸ç´åå·¦
}
// å¼¹åº
val := stack[len(stack)-1]
stack = stack[:len(stack)-1]
result = append(result, val.Val)
root = val.Right
}
return result
}
```
[clone-graph](https://leetcode-cn.com/problems/clone-graph/)
> ç»ä½ æ åè¿éå¾ä¸ä¸ä¸ªèç¹çå¼ç¨ï¼è¯·ä½ è¿å该å¾çæ·±æ·è´ï¼å
éï¼ã
```go
func cloneGraph(node *Node) *Node {
visited:=make(map[*Node]*Node)
return clone(node,visited)
}
// 1 2
// 4 3
// éå½å
éï¼ä¼ å
¥å·²ç»è®¿é®è¿çå
ç´ ä½ä¸ºè¿æ»¤æ¡ä»¶
func clone(node *Node,visited map[*Node]*Node)*Node{
if node==nil{
return nil
}
// å·²ç»è®¿é®è¿ç´æ¥è¿å
if v,ok:=visited[node];ok{
return v
}
newNode:=&Node{
Val:node.Val,
Neighbors:make([]*Node,len(node.Neighbors)),
}
visited[node]=newNode
for i:=0;i ç»å®ä¸ä¸ªç±  '1'ï¼éå°ï¼å '0'ï¼æ°´ï¼ç»æççäºç»´ç½æ ¼ï¼è®¡ç®å²å±¿çæ°éãä¸ä¸ªå²è¢«æ°´å
å´ï¼å¹¶ä¸å®æ¯éè¿æ°´å¹³æ¹åæåç´æ¹åä¸ç¸é»çéå°è¿æ¥èæçãä½ å¯ä»¥åè®¾ç½æ ¼çå个边å被水å
å´ã
æè·¯ï¼éè¿æ·±åº¦æç´¢éåå¯è½æ§ï¼æ³¨ææ 记已访é®å
ç´ ï¼
```go
func numIslands(grid [][]byte) int {
var count int
for i:=0;i=1{
count++
}
}
}
return count
}
func dfs(grid [][]byte,i,j int)int{
if i<0||i>=len(grid)||j<0||j>=len(grid[0]){
return 0
}
if grid[i][j]=='1'{
// æ è®°å·²ç»è®¿é®è¿(æ¯ä¸ä¸ªç¹åªéè¦è®¿é®ä¸æ¬¡)
grid[i][j]=0
return dfs(grid,i-1,j)+dfs(grid,i,j-1)+dfs(grid,i+1,j)+dfs(grid,i,j+1)+1
}
return 0
}
```
[largest-rectangle-in-histogram](https://leetcode-cn.com/problems/largest-rectangle-in-histogram/)
> ç»å® _n_ 个éè´æ´æ°ï¼ç¨æ¥è¡¨ç¤ºæ±ç¶å¾ä¸å个æ±åçé«åº¦ãæ¯ä¸ªæ±åå½¼æ¤ç¸é»ï¼ä¸å®½åº¦ä¸º 1 ã
> æ±å¨è¯¥æ±ç¶å¾ä¸ï¼è½å¤å¾ååºæ¥çç©å½¢çæå¤§é¢ç§¯ã
æè·¯ï¼æ±ä»¥å½åæ±å为é«åº¦çé¢ç§¯ï¼å³è½¬å为寻æ¾å°äºå½åå¼çå·¦å³ä¸¤è¾¹å¼

ç¨æ ä¿åå°äºå½åå¼çå·¦çå
ç´

```go
func largestRectangleArea(heights []int) int {
if len(heights) == 0 {
return 0
}
stack := make([]int, 0)
max := 0
for i := 0; i <= len(heights); i++ {
var cur int
if i == len(heights) {
cur = 0
} else {
cur = heights[i]
}
// å½åé«åº¦å°äºæ ï¼åå°æ å
å
ç´ é½å¼¹åºè®¡ç®é¢ç§¯
for len(stack) != 0 && cur <= heights[stack[len(stack)-1]] {
pop := stack[len(stack)-1]
stack = stack[:len(stack)-1]
h := heights[pop]
// 计ç®å®½åº¦
w := i
if len(stack) != 0 {
peek := stack[len(stack)-1]
w = i - peek - 1
}
max = Max(max, h*w)
}
// è®°å½ç´¢å¼å³å¯è·å对åºå
ç´
stack = append(stack, i)
}
return max
}
func Max(a, b int) int {
if a > b {
return a
}
return b
}
```
## Queue éå
常ç¨äº BFS 宽度ä¼å
æç´¢
[implement-queue-using-stacks](https://leetcode-cn.com/problems/implement-queue-using-stacks/)
> ä½¿ç¨æ å®ç°éå
```go
type MyQueue struct {
stack []int
back []int
}
/** Initialize your data structure here. */
func Constructor() MyQueue {
return MyQueue{
stack: make([]int, 0),
back: make([]int, 0),
}
}
// 1
// 3
// 5
/** Push element x to the back of queue. */
func (this *MyQueue) Push(x int) {
for len(this.back) != 0 {
val := this.back[len(this.back)-1]
this.back = this.back[:len(this.back)-1]
this.stack = append(this.stack, val)
}
this.stack = append(this.stack, x)
}
/** Removes the element from in front of queue and returns that element. */
func (this *MyQueue) Pop() int {
for len(this.stack) != 0 {
val := this.stack[len(this.stack)-1]
this.stack = this.stack[:len(this.stack)-1]
this.back = append(this.back, val)
}
if len(this.back) == 0 {
return 0
}
val := this.back[len(this.back)-1]
this.back = this.back[:len(this.back)-1]
return val
}
/** Get the front element. */
func (this *MyQueue) Peek() int {
for len(this.stack) != 0 {
val := this.stack[len(this.stack)-1]
this.stack = this.stack[:len(this.stack)-1]
this.back = append(this.back, val)
}
if len(this.back) == 0 {
return 0
}
val := this.back[len(this.back)-1]
return val
}
/** Returns whether the queue is empty. */
func (this *MyQueue) Empty() bool {
return len(this.stack) == 0 && len(this.back) == 0
}
/**
* Your MyQueue object will be instantiated and called as such:
* obj := Constructor();
* obj.Push(x);
* param_2 := obj.Pop();
* param_3 := obj.Peek();
* param_4 := obj.Empty();
*/
```
äºåæ 屿¬¡éå
```go
func levelOrder(root *TreeNode) [][]int {
// éè¿ä¸ä¸å±çé¿åº¦ç¡®å®ä¸ä¸å±çå
ç´
result := make([][]int, 0)
if root == nil {
return result
}
queue := make([]*TreeNode, 0)
queue = append(queue, root)
for len(queue) > 0 {
list := make([]int, 0)
// 为ä»ä¹è¦ålengthï¼
// è®°å½å½å屿å¤å°å
ç´ ï¼éåå½åå±ï¼åæ·»å ä¸ä¸å±ï¼
l := len(queue)
for i := 0; i < l; i++ {
// åºéå
level := queue[0]
queue = queue[1:]
list = append(list, level.Val)
if level.Left != nil {
queue = append(queue, level.Left)
}
if level.Right != nil {
queue = append(queue, level.Right)
}
}
result = append(result, list)
}
return result
}
```
[01-matrix](https://leetcode-cn.com/problems/01-matrix/)
> ç»å®ä¸ä¸ªç± 0 å 1 ç»æçç©éµï¼æ¾åºæ¯ä¸ªå
ç´ å°æè¿ç 0 çè·ç¦»ã
> 两个ç¸é»å
ç´ é´çè·ç¦»ä¸º 1
```go
// BFS ä»0è¿éåï¼å¼¹åºä¹å计ç®ä¸ä¸å·¦å³çç»æï¼å°ä¸ä¸å·¦å³éæ°è¿éåè¿è¡äºå±æä½
// 0 0 0 0
// 0 x 0 0
// x x x 0
// 0 x 0 0
// 0 0 0 0
// 0 1 0 0
// 1 x 1 0
// 0 1 0 0
// 0 0 0 0
// 0 1 0 0
// 1 2 1 0
// 0 1 0 0
func updateMatrix(matrix [][]int) [][]int {
q:=make([][]int,0)
for i:=0;i=0&&x=0&&y