前言
从现在开始,争取每天写一道题目,再将题目的解答思路分析出来,将每一类型的题归纳到一起去,这一篇主要写动态规划,将不断进行更新,欢迎一起探讨思路。
total
38
排序
问题1–把数组排成最小的数
https://leetcode-cn.com/problems/ba-shu-zu-pai-cheng-zui-xiao-de-shu-lcof/
输入一个非负整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。
在这里插入代码片
问题2–德州扑克
https://leetcode-cn.com/problems/bu-ke-pai-zhong-de-shun-zi-lcof/
从扑克牌中随机抽5张牌,判断是不是一个顺子,即这5张牌是不是连续的。2~10为数字本身,A为1,J为11,Q为12,K为13,而大、小王为 0 ,可以看成任意数字。A 不能视为 14。
func isStraight(nums []int) bool {
sort.Ints(nums)
var zero int
for i:=0;i<=len(nums)-2;i++{
if nums[i]==0{
zero++
}else{
cap:=nums[i+1]-nums[i] - 1
if cap > 0{
zero = zero - cap
if zero<0{
return false
}
}else if cap<0{
return false
}
}
}
return true
}
搜索与回溯
问题1–机器人运动范围
https://leetcode-cn.com/problems/ji-qi-ren-de-yun-dong-fan-wei-lcof/
地上有一个m行n列的方格,从坐标 [0,0] 到坐标 [m-1,n-1] 。一个机器人从坐标 [0, 0] 的格子开始移动,它每次可以向左、右、上、下移动一格(不能移动到方格外),也不能进入行坐标和列坐标的数位之和大于k的格子。例如,当k为18时,机器人能够进入方格 [35, 37] ,因为3+5+3+7=18。但它不能进入方格 [35, 38],因为3+5+3+8=19。请问该机器人能够到达多少个格子?
func getSUmForK(i,j int)int{
var sum int
for i>0{
sum = sum + i%10
i = i/10
}
for j>0{
sum = sum + j%10
j = j/10
}
return sum
}
//考虑矩阵的特性,右下角的值是矩阵的最大
//由于采用的是位数相加,所以无法找出数学规律,需要进行搜索
func innerMove(res *int,i,j,m,n,k int,path[][]bool){
//上可达,满足小于k,没有走到过
if i-1>=0&&getSUmForK(i-1,j)<=k&&path[i-1][j]==false{
*res = *res + 1
path[i-1][j] = true
innerMove(res,i-1,j,m,n,k,path)
}
//下
if i+1<m&&getSUmForK(i+1,j)<=k&&path[i+1][j]==false{
*res = *res + 1
path[i+1][j] = true
innerMove(res,i+1,j,m,n,k,path)
}
//左
if j-1>=0&&getSUmForK(i,j-1)<=k&&path[i][j-1]==false{
*res = *res + 1
path[i][j-1] = true
innerMove(res,i,j-1,m,n,k,path)
}
//右
if j+1<n&&getSUmForK(i,j+1)<=k&&path[i][j+1]==false{
*res = *res + 1
path[i][j+1] = true
innerMove(res,i,j+1,m,n,k,path)
}
}
func movingCount(m int, n int, k int) int {
path:=make([][]bool,m)
for i:=0;i<m;i++{
path[i] = make([]bool,n)
}
var res int
path[0][0] = true
res = 1
innerMove(&res,0,0,m,n,k,path)
return res
}
问题2–矩阵中路径
//采用暴力解决方法,遍历数组查看是否有符合的内容
func exist(board [][]byte, word string) bool {
if len(word) < 1 || len(board)==0{
return false
}
m:=len(board)
n:=len(board[0])
for i:=0;i<len(board);i++{
for j:=0;j<len(board[i]);j++{
if word[0] == board[i][j]{
if len(word)==1{
return true
}
path:=make([][]bool,m)
for i:=0;i<m;i++{
path[i] = make([]bool,n)
}
path[i][j] = true
if innerExist(board,word[1:],i,j,m,n,path){
return true
}
}
}
}
return false
}
func innerExist(board [][]byte,word string,i,j,m,n int,path [][]bool)bool{
//上边可以走,上面没有走过,且相等
if i-1>=0 && path[i-1][j]==false && word[0]==board[i-1][j]{
//满足条件
if len(word)==1{
return true
}else{
path[i-1][j] = true
if innerExist(board,word[1:],i-1,j,m,n,path){
return true
}
path[i-1][j] = false
}
}
//下边可以走
if i+1<m && path[i+1][j]==false && word[0]==board[i+1][j]{
//满足条件
if len(word)==1{
return true
}else{
path[i+1][j] = true
if innerExist(board,word[1:],i+1,j,m,n,path){
return true
}
path[i+1][j] = false
}
}
//左边
if j-1>=0 && path[i][j-1]==false && word[0]==board[i][j-1]{
//满足条件
if len(word)==1{
return true
}else{
path[i][j-1] = true
if innerExist(board,word[1:],i,j-1,m,n,path){
return true
}
path[i][j-1] = false
}
}
//右边
if j+1<n && path[i][j+1]==false && word[0]==board[i][j+1]{
//满足条件
if len(word)==1{
return true
}else{
path[i][j+1] = true
if innerExist(board,word[1:],i,j+1,m,n,path){
return true
}
path[i][j+1] = false
}
}
return false
}
问题3–二叉树和为某一值
https://leetcode-cn.com/problems/er-cha-shu-zhong-he-wei-mou-yi-zhi-de-lu-jing-lcof/
func innerPathSum(root *TreeNode, now,target int,arr []int,res *[][]int){
if root==nil{
return
}
if root.Val + now ==target&& root.Left==nil&&root.Right==nil{
arr = append(arr,root.Val)
result:=make([]int,0,len(arr))
for i:=0;i<len(arr);i++{
result = append(result,arr[i])
}
*res = append(*res,result)
return
}
if root.Left!=nil||root.Right!=nil {
arr = append(arr,root.Val)
innerPathSum(root.Left,now+root.Val,target,arr,res)
innerPathSum(root.Right,now+root.Val,target,arr,res