如題目: 回傳最長連續的1
In: binary array nums
Out: the maximum number of consecutive 1
's in the array
class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
maxLen = 0
tmpLen = 0
for i in nums:
if i == 1:
tmpLen+=1
maxLen=max(maxLen,tmpLen)
else:
tmpLen=0
return maxLen
class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
int tmpLen = 0;
int maxLen = 0;
for(int i=0;i<nums.length;i++)
{
if(nums[i]==1)
{
tmpLen++;
maxLen=Math.max(tmpLen,maxLen);
}
else
{
tmpLen=0;
}
}
return maxLen;
}
}
Easy