106 - 485最大连续1的个数

题目

给定一个二进制数组, 计算其中最大连续1的个数。

示例 1:

输入: [1,1,0,1,1,1] 输出: 3 解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3.

注意:

  • 输入的数组只包含 0 和1。

  • 输入数组的长度是正整数,且不超过 10,000。

解答

只能想到,用指针遍历,记录最大值

变成字符串,再根据0split

作者:QQqun902025048

链接:https://leetcode-cn.com/problems/max-consecutive-ones/solution/1xing-python-zhi-zhen-jie-fa-by-qqqun902025048/

/**
 * @param {number[]} nums
 * @return {number}
 */
var findMaxConsecutiveOnes = function (nums) {
  const str = nums.join('')
  const arr = str.split('0')
  let max = 0
  for (const item of arr) {
    if (item.length > max) {
      max = item.length
    }
  }
  return max
};

Runtime: 80 ms, faster than 12.81% of JavaScript online submissions for Max Consecutive Ones.

Memory Usage: 37.4 MB, less than 11.11% of JavaScript online submissions for Max Consecutive Ones.

class Solution:
    def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
        return len(max(''.join(map(str, nums)).split('0')))

Runtime: 428 ms, faster than 36.70% of Python3 online submissions for Max Consecutive Ones.

Memory Usage: 14.5 MB, less than 7.69% of Python3 online submissions for Max Consecutive Ones.

双变量

class Solution:
    def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
        ans = temp = 0
        for num in nums:
            if num:
                temp += 1
            else:
                ans = max(ans, temp)
                temp = 0
        return max(ans, temp)

Runtime: 404 ms, faster than 78.87% of Python3 online submissions for Max Consecutive Ones.

Memory Usage: 14.2 MB, less than 7.69% of Python3 online submissions for Max Consecutive Ones.

/**
 * @param {number[]} nums
 * @return {number}
 */
var findMaxConsecutiveOnes = function (nums) {
  let ans = 0, temp = 0
  for (const item of nums) {
    if (item) {
      temp += 1
    } else {
      ans = Math.max(ans, temp)
      temp = 0
    }
  }
  return Math.max(ans, temp)
};

Runtime: 72 ms, faster than 29.02% of JavaScript online submissions for Max Consecutive Ones.

Memory Usage: 37.4 MB, less than 22.22% of JavaScript online submissions for Max Consecutive Ones.

func max(a, b int) int {
    if a >= b {
        return a
    } else {
        return b
    }
}
func findMaxConsecutiveOnes(nums []int) int {
    var ans, temp int
    for _, value := range nums {
        if value == 1 {
            temp += 1
        } else {
            ans = max(ans, temp)
            temp = 0
        }
    }
    return max(ans, temp)
}

Runtime: 36 ms, faster than 94.33% of Go online submissions for Max Consecutive Ones.

Memory Usage: 6.3 MB, less than 100.00% of Go online submissions for Max Consecutive Ones.

动态规划

class Solution:
    def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
        if len(nums) == 0:
            return 0
        dp = [-1]*len(nums)
        if nums[0] == 1:
            dp[0] = 1
        else:
            dp[0] = 0
        for i in range(1, len(nums)):
            if nums[i] == 1:
                dp[i] = dp[i-1]+1 if dp[i-1] != -1 else 1
            else:
                dp[i] = 0
        return max(dp)

Runtime: 404 ms, faster than 64.84% of Python3 online submissions for Max Consecutive Ones.

Memory Usage: 13.2 MB, less than 88.46% of Python3 online submissions for Max Consecutive Ones.

Last updated

Was this helpful?