# Simple Python

• ``````class Solution(object):
def findMaxConsecutiveOnes(self, nums):
cnt = 0
ans = 0
for num in nums:
if num == 1:
cnt += 1
ans = max(ans, cnt)
else:
cnt = 0
return ans
``````

• is that right?

• This post is deleted!

``````def findMaxConsecutiveOnes(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
ans = 0;
cnt = 0;
for num in nums:
if num == 1:
cnt += 1;
else:
ans = max(ans,cnt);
cnt = 0;
ans=max(ans,cnt);
return ans;
``````

• @perry_C Both posts are right. The difference is that your solution is more efficient because you only compare the current count to the current maximum length when you encounter a '0'. But you need one more comparison when the for-loop is over. Althought efficient, your code is slightly more tedious than the original post. But this is the trade-off.

• @weih1214 said in Simple Python:

The difference is that your solution is more efficient because you only compare the current count to the current maximum length when you encounter a '0'

But that's only more efficient if there are more zeros than ones, no?

• @StefanPochmann Ooh, you're right. Thanks for your remind.

• This post is deleted!

• I think my code might be more efficient, because I recorded all counts and found the maximum when the loop was over.

``````class Solution(object):
def findMaxConsecutiveOnes(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
count = 0
ans = []
for num in nums:
if num == 1:
count += 1
else:
ans.append(count)
count = 0
ans.append(count)
return max(ans)

``````

• @myheine I don't think so. Your code requires N additional space to save `ans` and `max(ans)` needs N more steps to find a maximum value from `ans`.