当前位置:网站首页>【Leetcode】最大连续1的个数
【Leetcode】最大连续1的个数
2022-07-01 19:09:00 【小朱小朱绝不服输】
leetcode中最大连续1的个数类题目的汇总。
最大连续1的个数Ⅰ
1. 题目描述
leetcode题目链接:485. 最大连续 1 的个数
2. 思路分析
方法一:一次遍历
遇到nums[i] == 1
,则count++
,遇到nums[i] == 0
, 则count=0
,然后更新最大连续1的个数。
方法二:双指针
固定左边界,然后判断右边界,更新最大值和右边界减去左边界的差值。
方法三:动态规划
dp[i]表示以第i个元素结尾的最大连续1的个数。
其中:双指针方法运行时间最短。
3. 参考代码
方法一:一次遍历
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;
}
}
方法二:双指针
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;
}
}
方法三:动态规划
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;
}
}
最大连续1的个数Ⅱ
1. 题目描述
leetcode题目链接:Leetcode 487.最大连续1的个数Ⅱ,plus会员题目,直接看题。
给定一个二进制数组,你可以最多将 1 个 0 翻转为 1,找出其中最大连续 1 的个数。
输入:[1,0,1,1,0]
输出:4
解释:翻转第一个 0 可以得到最长的连续 1。
当翻转以后,最大连续 1 的个数为 4。
2. 思路分析
3. 参考代码
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;
}
}
最大连续1的个数Ⅲ
1. 题目描述
leetcode题目链接:1004. 最大连续1的个数 III
2. 思路分析
最大连续1的个数Ⅱ的进阶版,有k个0翻转为1。
3. 参考代码
边栏推荐
- [multithreading] realize the singleton mode (hungry and lazy) realize the thread safe singleton mode (double validation lock)
- EURA欧瑞E1000系列变频器使用PID实现恒压供水功能的相关参数设置及接线
- Summary of SQL aggregate query method for yyds dry goods inventory
- Richview RichEdit srichviewedit PageSize page setup and synchronization
- 300 linear algebra Lecture 4 linear equations
- STC 32位8051单片机开发实例教程 三 程序编译设置与下载
- 2022熔化焊接与热切割上岗证题目模拟考试平台操作
- EDA工具对芯片产业的重要性知识科普
- 如何用OpenMesh创建一个四棱锥
- C#联合halcon应用——大华相机采集类
猜你喜欢
[multithreading] realize the singleton mode (hungry and lazy) realize the thread safe singleton mode (double validation lock)
关于元宇宙下一代入口——脑机接口的实现
Exclusive news: Alibaba cloud quietly launched RPA cloud computer and has opened cooperation with many RPA manufacturers
How to create a pyramid with openmesh
Win11怎么关闭开机自启动软件
Detailed explanation and code example of affinity propagation clustering calculation formula based on graph
Myslq ten kinds of locks, an article will take you to fully analyze
喜马拉雅自研网关架构演进过程
EDA工具对芯片产业的重要性知识科普
3D全景模型展示可视化技术演示
随机推荐
Importance of EDA tools to chip industry knowledge popularization
目标检测——Yolo系列
收藏:存储知识全面总结
C # joint Halcon application - Dahua camera acquisition class
SwiftUI 4 新功能大全之 Toggle与 Mixed Toggle 多个绑定组件
关于元宇宙下一代入口——脑机接口的实现
Past and present life of product modular design
强大、好用、适合程序员/软件开发者的专业编辑器/笔记软件综合评测和全面推荐
Is it safe to open an account online? Can a novice open a stock trading account.
使用Zadig从0到1搭建持续交付平台
Stack Overflow 2022 开发者调查:行业走向何方?
What if win11 can't pause the update? Win11 pause update is gray. How to solve it?
2022/6/8-2022/6/12
Face recognition system opencv face detection
Entering Ruxin Town, digital intelligence transformation connects "future community"
2022安全员-B证考试练习题模拟考试平台操作
走进如心小镇,数智化变革连接“未来社区”
STC 32-bit 8051 single chip microcomputer development example tutorial three program compilation setting and download
300题线性代数 第四讲 线性方程组
STC 32-bit 8051 single chip microcomputer development example tutorial II i/o working mode and its configuration