当前位置:网站首页>[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
边栏推荐
- 二叉搜索树的创建,查找,添加,删除操作
- From 20s to 500ms, I used these three methods
- LeetCode中等题题分享(5)
- PWN attack and defense world cgpwn2
- Use the htaccess file to prohibit the script execution permission in the directory
- sso单点登录的实现。
- Relevant settings of wechat applet cache expiration time (recommended)
- Kuberntes cloud native combat high availability deployment architecture
- Some understandings of graph convolution neural network r-gcn considering relations and some explanations of DGL official code
- [template] adaptive Simpson integral
猜你喜欢
Leetcode skimming: stack and queue 05 (inverse Polish expression evaluation)
The new version of graphic network PDF will be released soon
Leetcode skimming: stack and queue 06 (top k high-frequency elements)
Kuberntes cloud native combat high availability deployment architecture
使用多线程Callable查询oracle数据库
Correlation - intra group correlation coefficient
How to type spaces in latex
leetcode96不同的二叉搜索樹
[opencv450] hog+svm and hog+cascade for pedestrian detection
SQL数据分析之流程控制语句【if,case...when详解】
随机推荐
【CTF】bjdctf_2020_babystack2
Leetcode medium question sharing (5)
Openvino model performance evaluation tool DL workbench
【微信授权登录】uniapp开发小程序,实现获取微信授权登录功能
Kyushu cloud and Intel jointly released the smart campus private cloud framework, enabling new infrastructure for education
Kuberntes cloud native combat high availability deployment architecture
@Valid参数校验不生效
Some understandings of graph convolution neural network r-gcn considering relations and some explanations of DGL official code
Linux centos7 installation Oracle11g super perfect novice tutorial
数据库--SqlServer详解
Example explanation: move graph explorer to jupyterlab
挖财学堂开户打新债安全可靠嘛?
JS -- image to base code, base to file object
Linux CentOS7安装Oracle11g的超完美新手教程
Use es to realize epidemic map or take out order function (including code and data)
Leetcode skimming: stack and queue 05 (inverse Polish expression evaluation)
Shell custom function
If the browser is accidentally closed, how does react cache the forms filled out by users?
求逆序数的三个方法
Correlation - intra group correlation coefficient