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.

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.

双变量

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.

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.

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.

动态规划

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?