485. Max Consecutive Ones (最大连续数) by Python

485. Max Consecutive Ones

题目:

Given a binary array, find the maximum number of consecutive 1s in this array.

Example 1:

Input: [1,1,0,1,1,1]
Output: 3
Explanation: The first two digits or the last three digits are consecutive 1s.
    The maximum number of consecutive 1s is 3.

Note:

  • The input array will only contain 0 and 1.
  • The length of input array is a positive integer and will not exceed 10,000

给你一个二位数列表,求出列表最大的1连续的数目

思路:

很简单的列表遍历,要注意的就是最后一次遍历的值也要进行比较

结果:

class Solution(object):    def findMaxConsecutiveOnes(self, nums):        """        :type nums: List[int]        :rtype: int        """        max_num = 0        statistics = 0        # 遍历列表        for temp in nums:            if temp == 1:                statistics += 1            else:                if statistics > max_num:                    max_num = statistics                statistics = 0        # 保证最后一遍也能进行比较        if statistics > max_num:            max_num = statistics        return max_num
时间: 2024-12-20 06:38:49

485. Max Consecutive Ones (最大连续数) by Python的相关文章

485 Max Consecutive Ones 最大连续1的个数

给定一个二进制数组, 计算其中最大连续1的个数.示例 1:输入: [1,1,0,1,1,1]输出: 3解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3.注意:    输入的数组只包含 0 和1.    输入数组的长度是正整数,且不超过 10,000.详见:https://leetcode.com/problems/max-consecutive-ones/description/ C++: class Solution { public: int findMaxConsecu

[LeetCode OJ]485. Max Consecutive Ones

Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1,1,0,1,1,1] Output: 3 Explanation: The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3. Note: T

【leetcode】485. Max Consecutive Ones

problem 485. Max Consecutive Ones solution1: class Solution { public: int findMaxConsecutiveOnes(vector<int>& nums) { if(nums.empty()) return 0; int ans = 0, max = INT_MIN; for(int i=0; i<nums.size(); i++) { if(nums[i]==1) ans++; else if(nums

485. Max Consecutive Ones最长的连续1的个数

[抄题]: Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1,1,0,1,1,1] Output: 3 Explanation: The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3. [

[Array]485. Max Consecutive Ones

Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1,1,0,1,1,1] Output: 3 Explanation: The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3. Note: T

485. Max Consecutive Ones

Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1,1,0,1,1,1] Output: 3 Explanation: The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3. Note: T

LeetCode_485. Max Consecutive Ones

485. Max Consecutive Ones Easy Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1,1,0,1,1,1] Output: 3 Explanation: The first two digits or the last three digits are consecutive 1s. The maximum number

待字闺中之最多连续数的子集

题目来源,待字闺中,原创@陈利人 ,欢迎大家继续关注微信公众账号"待字闺中" 给一个整数数组a[]. 找到当中包括最多连续数的子集,比方给:15, 7, 12, 6, 14, 13, 9, 11,则返回: 5:[11, 12, 13, 14, 15] . 分析:最简单的方法是sort然后scan一遍,可是要 o(nlgn) , 有什么 O(n) 的方法吗?网上有人用map或set来做.但map或set的复杂度还是O(nlgn).并查集能够做到O(n),此题用并查集比較简单. 下面參考h

百度之星 2015 初赛(1) 1002 找连续数

找连续数 Accepts: 401 Submissions: 1911 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Description 小度熊拿到了一个无序的数组,对于这个数组,小度熊想知道是否能找到一个k 的区间,里面的 k 个数字排完序后是连续的. 现在小度熊增加题目难度,他不想知道是否有这样的 k 的区间,而是想知道有几个这样的 k 的区间. Input