当前位置:网站首页>[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
边栏推荐
- [template] adaptive Simpson integral
- Using multithreaded callable to query Oracle Database
- How to improve data quality
- Windows installation WSL (II)
- Selectively inhibiting learning bias for active sampling
- Digital transformation has a long way to go, so how to take the key first step
- 数据分析方法论与前人经验总结【笔记干货】
- LeetCode中等题题分享(5)
- Data analysis methodology and previous experience summary [notes dry goods]
- SQL Server 安裝指南
猜你喜欢

数据库--SqlServer详解

Graduation season | Huawei experts teach the interview secret: how to get a high paying offer from a large factory?

4. Object mapping Mapstercover

SQL Server 安装指南

What is ThreadLocal memory leak and how to solve it

数据分析方法论与前人经验总结【笔记干货】

下载在线视频 m3u8使用教程

Ldr6035 smart Bluetooth audio can continuously charge and discharge mobile devices

The new version of graphic network PDF will be released soon

How to type spaces in latex
随机推荐
[opencv450] hog+svm and hog+cascade for pedestrian detection
[template] adaptive Simpson integral
An intern's journey to cnosdb
Mysql database driver (JDBC Driver) jar package download
Using multithreaded callable to query Oracle Database
ERP项目施行计划的目的是什么?
在证券账户上买基金安全吗?哪里可以买基金
Is the securities account given by qiniu business school safe? Where can I open an account
Node——添加压缩文件
Dongge cashes in and the boss retires?
启牛学院开户安全的吗?开户怎么开?
Record the accidental success and failure of uploading large files
Leetcode96 different binary search trees
Intelligent operation and maintenance practice: banking business process and single transaction tracking
Which app is better and more secure for stock mobile account opening
Is it safe to choose mobile phone for stock trading account opening in Beijing?
Linux CentOS7安装Oracle11g的超完美新手教程
449 original code, complement code, inverse code
Asp .NetCore 微信订阅号自动回复之文本篇
2022 pinduoduo details / pinduoduo product details / pinduoduo SKU details