Skip to content

Latest commit

 

History

History
57 lines (53 loc) · 1.79 KB

File metadata and controls

57 lines (53 loc) · 1.79 KB

485. 最大连续1的个数

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

Video

<iframe width="100%" height="600" src="//player.bilibili.com/player.html?aid=541428524&bvid=BV1bi4y137XD&cid=216235112&page=1&as_wide=1&high_quality=1" scrolling="no" border="0" frameborder="no" framespacing="0" allowfullscreen="true"> </iframe>

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