当前位置:网站首页>[leetcode] number of maximum consecutive ones
[leetcode] number of maximum consecutive ones
2022-07-02 00:33:00 【Xiao Zhu, Xiao Zhu will never admit defeat】
leetcode Medium maximum continuous 1 A summary of several categories of topics .
List of articles
Maximum continuous 1 The number of Ⅰ
1. Title Description
leetcode Topic link :485. Maximum continuous 1 The number of 
2. Thought analysis
Method 1 : One traverse
encounter nums[i] == 1, be count++, encounter nums[i] == 0, be count=0, Then update the maximum continuous 1 The number of .
Method 2 : Double pointer
Fixed left boundary , Then judge the right boundary , Update the difference between the maximum value and the right boundary minus the left boundary .
Method 3 : Dynamic programming
dp[i] Says to the first i Maximum continuity at the end of elements 1 The number of .
among : The double pointer method has the shortest running time .
3. Reference code
Method 1 : One traverse
class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
int res = 0, count = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 1) {
count++;
} else {
count = 0;
}
res = Math.max(res, count);
}
return res;
}
}
Method 2 : Double pointer
class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
int res = 0;
for (int i = 0; i < nums.length; i++) {
int j = i;
while (j < nums.length && nums[j] == 1) {
j++;
}
res = Math.max(res, j - i);
i = j;
}
return res;
}
}
Method 3 : Dynamic programming
class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
int n = nums.length;
int[] dp = new int[n + 1];
int res = 0;
for (int i = 1; i <= n; i++) {
if (nums[i - 1] == 1) {
dp[i] = dp[i - 1] + 1;
res = Math.max(res, dp[i]);
}
}
return res;
}
}
Maximum continuous 1 The number of Ⅱ
1. Title Description
leetcode Topic link :Leetcode 487. Maximum continuous 1 The number of Ⅱ,plus Membership title , Look at the problem directly .
Given a binary array , You can at most 1 individual 0 Flip to 1, Find the largest one of them 1 The number of .
Input :[1,0,1,1,0]
Output :4
explain : Flip the first 0 You can get the longest continuous 1.
When flipped , Maximum continuous 1 The number of 4.
2. Thought analysis
3. Reference code
class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
int left = 0, right = 0;
int res = 0, count = 0;
while (right < nums.length) {
if (nums[right++] == 0) {
count++;
}
while (count > 1) {
if (nums[left++] == 0) {
count--;
}
}
res = Math.max(res, right - left);
}
return res;
}
}
Maximum continuous 1 The number of Ⅲ
1. Title Description
leetcode Topic link :1004. Maximum continuous 1 The number of III
2. Thought analysis
Maximum continuous 1 The number of Ⅱ An advanced version of , Yes k individual 0 Flip to 1.
3. Reference code
边栏推荐
- Jielizhi, production line assembly link [chapter]
- SQL Server 安装指南
- Is it safe to buy funds in a securities account? Where can I buy funds
- [template] adaptive Simpson integral
- Jielizhi, production line assembly link [chapter]
- [CTF] bjdctf 2020 Bar _ Bacystack2
- SQL Server Installation Guide
- Review data desensitization system
- Flow control statement of SQL data analysis [if, case... When detailed]
- 记录一下大文件上传偶然成功偶然失败问题
猜你喜欢

heketi 记录
![Flow control statement of SQL data analysis [if, case... When detailed]](/img/7b/eabb0700936d34a3a145737580be88.png)
Flow control statement of SQL data analysis [if, case... When detailed]

BPR (Bayesian personalized sorting)

时间复杂度与空间复杂度

LeetCode中等题题分享(5)

Example explanation: move graph explorer to jupyterlab

Practical calculation of the whole process of operational amplifier hysteresis comparator

【微信授权登录】uniapp开发小程序,实现获取微信授权登录功能

Intelligent operation and maintenance practice: banking business process and single transaction tracking

Review data desensitization system
随机推荐
RFID让固定资产盘点更快更准
挖财学堂开户打新债安全可靠嘛?
449-原码、补码、反码
Barbie q! How to analyze the new game app?
Ldr6035 smart Bluetooth audio can be charged and released (5.9.12.15.20v) fast charging and fast releasing device charging
EMC circuit protection device for surge and impulse current protection
I want to ask, which is the better choice for securities companies? I don't understand. Is it safe to open an account online now?
【底部弹出-选择器】uniapp Picker组件——底部弹起的滚动选择器
4. Object mapping Mapstercover
mysql之B tree 以及 B+tree
Kyushu cloud and Intel jointly released the smart campus private cloud framework, enabling new infrastructure for education
求逆序数的三个方法
What does open loop and closed loop mean?
Node——Egg 创建本地文件访问接口
USB-IF协会与各种接口的由来
LeetCode 0241.为运算表达式设计优先级 - DFS
Ldr6035 smart Bluetooth audio can continuously charge and discharge mobile devices
Graduation season | Huawei experts teach the interview secret: how to get a high paying offer from a large factory?
I would like to ask, which securities is better for securities account opening? Is it safe to open a mobile account?
Openvino model performance evaluation tool DL workbench