然后用result的存储最大的count,并和最后的count对比,返回最大的count ## 精简版本 class Solution: def findMaxConsecutiveOnes(self, nums: List[int]) -> int: max_count = count = 0 for num in nums: if num == 1: count += 1 max_count = max(max_count, count) else: count = 0 #...
遍历一次数组,以每个1 为起点向后数,数到0 时比较当前1的个数和最大1 的个数,然后将遍历的起点放到当前0 的后面。 3、代码 1intfindMaxConsecutiveOnes(vector<int>&nums) {2intresult =0;3if( nums.size() ==0)returnresult;45for(inti =0; i < nums.size() ; i++){6if( nums[i] ==1){...
FindHeaderBarSize FindTabBarSize FindBorderBarSize Given a binary arraynums, returnthe maximum number of consecutive1's in the array. Example 1: Input:nums = [1,1,0,1,1,1]Output:3Explanation:The first two digits or the last three digits are consecutive 1s. The maximum number of consecuti...
[LeetCode] 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:...
LeetCode-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....
题目地址:https://leetcode.com/problems/max-consecutive-ones-iii/ 题目描述 Given an arrayAof 0s and 1s, we may change up toKvalues from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s. Example 1: ...
// #414 Description: Third Maximum Number | LeetCode OJ 解法1:水题。 // Solution 1: Easy. 代码1 // Code 1 415 Add Strings // #415 高精度加法 描述:如题。 // #415 Description: Add Strings | LeetCode OJ 解法1:水题。 // Solution 1: Easy. 代码1 // Code 1 416 Partition Equal...
3165.Maximum-Sum-of-Subsequence-With-Non-adjacent-Elements (H) 3187.Peaks-in-Array (M+) 3261.Count-Substrings-That-Satisfy-20K-Constraint-II (H-) [Binary Index Tree] 307.Range-Sum-Query-Mutable (M) 1649.Create-Sorted-Array-through-Instructions (H) 2031.Count-Subarrays-With-More-Ones-Tha...
124Binary Tree Maximum Path SumPythonRecursion O(n) and O(n), max (left + node, right + node, left + node + right) 125Valid PalindromePythonExclude non-alphanumeric characters and compare O(n) 128Longest Consecutive SequencePythonSet or hash, pop adjacency, O(n) and O(n) ...
LeetCode-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....