力扣2道简单题(485 最大连续 1 的个数和566重塑矩阵)
作者:互联网
//485最大连续1的个数
func findMaxConsecutiveOnes(nums []int) (maxCnt int) {
cnt := 0
if len(nums) < 1 {
return
}
for _, v := range nums {
if v == 1 {
cnt++
} else {
maxCnt = max(maxCnt, cnt)
cnt = 0
}
}
maxCnt = max(maxCnt, cnt)
return
}
func max(x, y int) int {
if y > x {
return y
}
return x
}
//566重塑矩阵
func matrixReshape(nums [][]int, r int, c int) [][]int {
result := make([][]int, r) //初始化二维数组
for i := range result {
result[i] = make([]int, c)
}
hang := len(nums)
lie := len(nums[0])
if hang*lie != r*c {
return nums
}
for i := 0; i < hang*lie; i++ {
result[i/c][i%c] = nums[i/hang][i%lie] //行列规律小技巧
}
fmt.Println(result)
return result
}
标签:cnt,return,nums,int,566,力扣,result,maxCnt,485 来源: https://blog.csdn.net/github_34457546/article/details/113837213