当前位置:网站首页>【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. 参考代码
边栏推荐
- [Mysql]安装Mysql5.7
- SwiftUI 4 新功能大全之 Toggle与 Mixed Toggle 多个绑定组件
- 基于图的 Affinity Propagation 聚类计算公式详解和代码示例
- 使用Zadig从0到1搭建持续交付平台
- 三菱PLC FX3U脉冲轴点动功能块(MC_Jog)
- 随机头像大全,多分类带历史记录微信小程序源码_支持流量主
- 《软件工程导论(第六版)》 张海藩 复习笔记
- How to turn off the boot auto start software in win11
- cocoaPods 添加成功后,导入不了头文件或者not found file 报错
- How to connect the two nodes of the flow chart
猜你喜欢

Importance of EDA tools to chip industry knowledge popularization

What if the win11 shortcut key switching input method doesn't respond? Shortcut key switching input method does not respond

After adding cocoapods successfully, the header file cannot be imported or an error is reported in not found file

Items in richview documents

How to create a pyramid with openmesh

深度学习 神经网络基础

Uniapp uses Tencent map to select points without window monitoring to return users' location information. How to deal with it

关联线探究,如何连接流程图的两个节点

Simple but modern server dashboard dashdot

math_ Use differentiation to calculate approximate value
随机推荐
Iframe parent-child page communication
Stack overflow 2022 developer survey: where is the industry going?
GaussDB(for MySQL) :Partial Result Cache,通过缓存中间结果对算子进行加速
关联线探究,如何连接流程图的两个节点
极客DIY开源方案分享——数字幅频均衡功率放大器设计(实用的嵌入式电子设计作品软硬件综合实践)
【let var const】
漏洞复现-.Net-ueditor上传
寫博客文檔
编译原理复习笔记
【蓝桥杯Web】2022年第十三届蓝桥杯Web大学组国赛真题解析
Internship: complex JSON format data compilation interface
Review notes of Zhang Haifan in introduction to software engineering (Sixth Edition)
Importance of EDA tools to chip industry knowledge popularization
How to create a pyramid with openmesh
math_ Use differentiation to calculate approximate value
Error in installing sharp
Common components of flask
What else do you not know about new set()
STC 32-bit 8051 single chip microcomputer development example tutorial II i/o working mode and its configuration
Customize the insertion of page labels and realize the initial search of similar address books