-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path485. Max Consecutive Ones.py
More file actions
70 lines (57 loc) · 1.76 KB
/
485. Max Consecutive Ones.py
File metadata and controls
70 lines (57 loc) · 1.76 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
给定一个二进制数组, 计算其中最大连续1的个数。
示例 1:
输入: [1,1,0,1,1,1]
输出: 3
解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3.
注意:
输入的数组只包含 0 和1。
输入数组的长度是正整数,且不超过 10,000。
"""
# 最大的0或1的个数
# class Solution(object):
# def findMaxConsecutiveOnes(self, nums):
# """
# :type nums: List[int]
# :rtype: int
# """
# zero_number = 1
# ones_number = 1
# max_zero = 1
# max_one = 1
# for i in range(1, len(nums)):
# if nums[i] == nums[i - 1]:
# if nums[i]:
# ones_number += 1
# else:
# zero_number += 1
# else:
# zero_number = 1
# ones_number = 1
# max_zero = zero_number if zero_number > max_zero else max_zero
# max_one = ones_number if ones_number > max_one else max_one
# return max(max_zero, max_one)
# 最大的1的个数
class Solution(object):
def findMaxConsecutiveOnes(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
one_number = -1
max_one = -1
if nums.count(1):
for i in range(1, len(nums)):
if nums[i] and nums[i] == nums[i - 1]:
one_number += 1
else:
one_number = 1
max_one = one_number if one_number > max_one else max_one
return max_one
else:
return 0
if __name__ == '__main__':
test = [0]
print Solution().findMaxConsecutiveOnes(test)