岛屿的数量 岛屿数量
解题思路 可以将grid
看作是一个有向图 ,每个节点最多有上下左右四条边 ,竖直或水平相邻的 1 之间有边相连 ,那么岛屿的数量就是图的连通分量个数 。所以可以进行图的遍历,来求得连通分量的个数:
图的深度优先遍历
图的广度优先遍历
解题代码
图的深度优先遍历
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 func numIslands (grid [][]byte ) int { var landsNum int for i := 0 ; i < len (grid); i++ { for j := 0 ; j < len (grid[0 ]); j++ { if grid[i][j] == '1' { dfs(grid, i, j) landsNum++ } } } return landsNum } func dfs (grid [][]byte , i int , j int ) { if i < 0 || i > len (grid)-1 || j < 0 || j > len (grid[0 ])-1 { return } if grid[i][j] == '0' { return } grid[i][j] = '0' dfs(grid, i-1 , j) dfs(grid, i+1 , j) dfs(grid, i, j-1 ) dfs(grid, i, j+1 ) }
广度优先遍历
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 func numIslands (grid [][]byte ) int { var landsNum int for i := 0 ; i < len (grid); i++ { for j := 0 ; j < len (grid[0 ]); j++ { if grid[i][j] == '1' { bfs(grid, i, j) landsNum++ } } } return landsNum } func bfs (grid [][]byte , i int , j int ) { queue := [][]int {} queue = append (queue, []int {i, j}) for len (queue) > 0 { cur := queue[0 ] i = cur[0 ] j = cur[1 ] queue = queue[1 :] if i < 0 || i > len (grid)-1 || j < 0 || j > len (grid[0 ])-1 { continue } if grid[i][j] == '0' { continue } grid[i][j] = '0' queue = append (queue, []int {i-1 , j}) queue = append (queue, []int {i+1 , j}) queue = append (queue, []int {i, j-1 }) queue = append (queue, []int {i, j+1 }) } }
最长上升子序列 最长上升子序列
解题思路 利用动态规划 ,设dp[i]
为以nums[i]
为结尾的最长上升子序列(被初始化为1),则对于j < i
且nums[j] < nums[i]
,dp[i] = max{ dp[j] }+1
。那么最长上升子序列的长度为max{ dp[i] }, 0<= i< len(dp)
解题代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 func lengthOfLIS (nums []int ) int { if len (nums) == 0 { return 0 } dp := make ([]int , len (nums)) dp[0 ] = 1 maxLength := 1 for i := 1 ; i < len (nums); i++ { dp[i] = 1 for j := 0 ; j < i; j++ { if nums[j] < nums[i] { dp[i] = max(dp[j]+1 , dp[i]) } } maxLength = max(dp[i], maxLength) } return maxLength } func max (n1, n2 int ) int { if n1 > n2 { return n1 } return n2 }
用栈实现队列 用栈实现队列
解题思路 将一个栈当作输入栈 ,用于压入push
传入的数据;另一个栈当作输出栈 ,用于pop
和peek
操作。
每次pop
或peek
时,若输出栈为空则将输入栈的全部数据依次弹出并压入输出栈,这样输出栈从栈顶往栈底的顺序就是队列从队首往队尾的顺序。
解题代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 type MyQueue struct { inStack, outStack []int } func Constructor () MyQueue { return MyQueue{} } func (this *MyQueue) Push (x int ) { this.inStack = append (this.inStack, x) } func (this *MyQueue) in2out () { for len (this.inStack) > 0 { this.outStack = append (this.outStack, this.inStack[len (this.inStack)-1 ]) this.inStack = this.inStack[:len (this.inStack)-1 ] } } func (this *MyQueue) Pop () int { var ret int if len (this.outStack) == 0 { this.in2out() } ret = this.outStack[len (this.outStack)-1 ] this.outStack = this.outStack[:len (this.outStack)-1 ] return ret } func (this *MyQueue) Peek () int { if len (this.outStack) == 0 { this.in2out() } return this.outStack[len (this.outStack)-1 ] } func (this *MyQueue) Empty () bool { if len (this.inStack) == 0 && len (this.outStack) == 0 { return true } return false }