problem:https://leetcode.com/problems/max-consecutive-ones-iii/ 维护最多包含k个0的滑动窗口,一旦超过了k个0,把队首的0 pop出来。不断更新当前滑动窗口中的数据个数,并取最大值返回即可。 classSolution {public:intlongestOnes(vector<int>& A,intK) {intcount =0;intindex = -1; deque<int>zeros;...
classSolution{public:intlongestOnes(vector<int>& A,intK){inti =0, j =0;intcur =0;intsz = A.size();intres =0;while(i < sz){while(j < sz && cur <= K){if(A[j] ==0){ cur++; }if(cur <= K){ j++; } } res =max(res, j - i);// cout<<i<<", "<<j<<","<<...
题目地址: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: Input: A = [1,1,1,0,0,0,1,1,1,1,0], K...
LeetCode中有如下一些题目,求的是连续出现的数字或者字母最长可以组成的子串长度,本质上还是滑动窗口类型的题目。 485. Max Consecutive Ones 487. Max Consecutive Ones II 1004. Max Consecutive Ones III 830. Positions of Large Groups sliding window相关题目 LeetCode 题目总结...
max(...): 最后,使用max函数找出这些长度中的最大值。在我们的例子中,最大长度是2。 解法三:分组计数 classSolution:deffindMaxConsecutiveOnes(self,nums:List[int])->int:groups=[[k,sum(1foriing)]fork,gingroupby(nums)]returnmax((vfork,vingroupsifk==1),default=0) ...
// #485 Description: Max Consecutive Ones | LeetCode OJ 解法1:水题。 // Solution 1: Trivial. 代码1 // Code 1 486 Predict the Winner // #486 预测赢家 描述:给定一个数组,每个元素表示一个分数。俩人轮流,每次从左端或右端拿走一个分数,如此直到拿完。分高的人赢。问先手是否必胜? // #486 ...
1004.Max-Consecutive-Ones-III (M) 1052.Grumpy-Bookstore-Owner (M) 1358.Number-of-Substrings-Containing-All-Three-Characters (M) 1838.Frequency-of-the-Most-Frequent-Element (H-) 395.Longest-Substring-with-At-Least-K-Repeating-Characters (H) 1763.Longest-Nice-Substring (H) 2009.Minimum-Numbe...
1004 Max Consecutive Ones III Medium Go 1021 Remove Outermost Parentheses Easy 1045 Customers Who Bought All Products Medium MySQL 1047 Remove All Adjacent Duplicates In String Easy Go 1056 Confusing Number 🔒 Easy Go 1068 Product Sales Analysis I Easy MySQL 1070 Product Sales Analysis III Medium...
1004 Max Consecutive Ones III Go 59.1% Medium 1005 Maximize Sum Of Array After K Negations Go 51.3% Easy 1006 Clumsy Factorial 53.3% Medium 1007 Minimum Domino Rotations For Equal Row 50.0% Medium 1008 Construct Binary Search Tree from Preorder Traversal 78.4% Medium 1009 Complement of...
func findMaxConsecutiveOnes(_ nums: [Int]) -> Int { var count = 0,maxCount = 0 var i = 0 while i < nums.count { while i < nums.count && nums[i] == 1{ count += 1 i += 1 } if count > maxCount { maxCount = count ...