485 最大连续1的个数

本文最后更新于:2022年4月9日 中午

leetcode2

给定一个二进制数组, 计算其中最大连续1的个数。

示例 1:

1
2
3
输入: [1,1,0,1,1,1]
输出: 3
解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3.

注意:

  • 输入的数组只包含 01
  • 输入数组的长度是正整数,且不超过 10,000。

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# @lc code=start
class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
count =0
max_cnt =0
for i in nums:
if i == 1:
count+=1
if count >max_cnt:
max_cnt = count
else:
count = 0
return max_cnt

# @lc code=end

@Jam

1
2
3
class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
return len(max('',join(map(str,nums)).split('0')))

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!