TreeviewCopyright © aleen42 all right reserved, powered by aleen42

485. 最大连续1的个数

https://leetcode-cn.com/problems/max-consecutive-ones/

Video

Java

/*
 * @Author: Goog Tech
 * @Date: 2020-07-24 16:51:42
 * @Description: https://leetcode-cn.com/problems/max-consecutive-ones/
 * @FilePath: \leetcode-googtech\#485. Max Consecutive Ones\Solution.java
 */ 
class Solution {
    public int findMaxConsecutiveOnes(int[] nums) {
        // 初始化变量
        int result = 0,count = 0;
        // 遍历数组中的元素
        for(int i = 0;i < nums.length;i++) {
            // 统计数组中的连续相同元素的个数
            count = nums[i] == 1 ? count+1 : 0;
            // 若count为0则表示数组中无连续相同的元素
            result = count > result ? count : result;
        }
        // 返回结果
        return result;
    }
}

Python

'''
@Author: Goog Tech
@Date: 2020-07-24 16:51:47
@Description: https://leetcode-cn.com/problems/max-consecutive-ones/
@FilePath: \leetcode-googtech\#485. Max Consecutive Ones\Solution.py
'''
class Solution(object):
    def findMaxConsecutiveOnes(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        # 初始化变量
        result = count = 0;
        # 遍历数组中的元素
        for num in nums:
            # 统计数组中连续相同元素的个数
            count = count+1 if num == 1 else 0;
            # 若count为0则表示数组中无连续相同的元素
            result = count if count > result else result
        # 返回结果
        return result
Copyright © GoogTech 2021 all right reserved,powered by GitbookLast update time : 2021-09-15 01:55:05

results matching ""

    No results matching ""