LeetCode: Max Consecutive Ones
这题最关键的是处理最开始连续1和最后连续1的方式,想到list一般在最前面加个node的处理方式,在最前面和最后面加0即可以很好地处理了
public class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
int[] newNums = new int[nums.length+2];
newNums[0] = 0;
newNums[newNums.length-1] = 0;
for (int i = 1; i < newNums.length-1; i++) {
newNums[i] = nums[i-1];
}
int ans = 0;
int lastPos = 0;
for (int i = 0; i < newNums.length; i++) {
if (newNums[i] == 0) {
ans = Math.max(ans, i - lastPos);
lastPos = i;
}
}
return ans - 1;
}
}
LeetCode: Max Consecutive Ones的更多相关文章
- LeetCode——Max Consecutive Ones
LeetCode--Max Consecutive Ones Question Given a binary array, find the maximum number of consecutive ...
- [LeetCode] Max Consecutive Ones II 最大连续1的个数之二
Given a binary array, find the maximum number of consecutive 1s in this array if you can flip at mos ...
- LeetCode Max Consecutive Ones II
原题链接在这里:https://leetcode.com/problems/max-consecutive-ones-ii/ 题目: Given a binary array, find the ma ...
- Leetcode: Max Consecutive Ones II(unsolved locked problem)
Given a binary array, find the maximum number of consecutive 1s in this array if you can flip at mos ...
- [LeetCode] Max Consecutive Ones 最大连续1的个数
Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1, ...
- LeetCode 1004. Max Consecutive Ones III
原题链接在这里:https://leetcode.com/problems/max-consecutive-ones-iii/ 题目: Given an array A of 0s and 1s, w ...
- 【leetcode】485. Max Consecutive Ones
problem 485. Max Consecutive Ones solution1: class Solution { public: int findMaxConsecutiveOnes(vec ...
- 485. Max Consecutive Ones - LeetCode
Question 485. Max Consecutive Ones Solution 题目大意:给一个数组,取连续1的最大长度 思路:遍历数组,连续1就加1,取最大 Java实现: public i ...
- LeetCode——Longest Consecutive Sequence
LeetCode--Longest Consecutive Sequence Question Given an unsorted array of integers, find the length ...
随机推荐
- 转:Python的这几个技巧,简直屌爆了
经使用Python编程有多年了,即使今天我仍然惊奇于这种语言所能让代码表现出的整洁和对DRY编程原则的适用.这些年来的经历让我学到了很多的小技巧和知识,大多数是通过阅读很流行的开源软件,如Django ...
- sql语句中3表删除和3表查询
好久没来咱们博客园了,主要近期在忙一些七七八八的杂事,包括打羽毛球比赛的准备和自己在学jqgrid的迷茫.先不扯这些没用的了,希望大家能记得小弟,小弟在此谢过大家了. 回归正题:(以下的sql是本人在 ...
- redis 命令行 操作
redis目前提供四种数据类型:string,list,set及zset(sorted set). * string是最简单的类型,你可以理解成与Memcached一模一个的类型,一个key对应一个v ...
- 蓝桥杯 第四届C/C++预赛真题(2) 马虎的算式(穷举)
标题: 马虎的算式 小明是个急性子,上小学的时候经常把老师写在黑板上的题目抄错了. 有一次,老师出的题目是:36 x 495 = ? 他却给抄成了:396 x 45 = ? 但结果却很戏剧性,他的答案 ...
- MathType与Origin是怎么兼容的
MathType作为一款常用的公式编辑器,可以与很多的软件兼容使用.Origin虽然是一款专业绘图与数据分析软件,但是在使用过程中也是可以用到MathType.它可以帮助Origin给图表加上标签,或 ...
- JZOJ.5275【NOIP2017模拟8.14】水管
Description
- [LintCode] 通配符查询
动态规划: class Solution { public: /** * @param s: A string * @param p: A string includes "?" ...
- POJ 1423 Greatest Common Increasing Subsequence【裸LCIS】
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1423 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- Linux基础命令(二)
作业一:1) 新建用户natasha,uid为1000,gid为555,备注信息为“master” groupadd -g 555 netasha useradd -u 1000 -g netasha ...
- centos7开机启动tomcat7
1.进入tomcat/bin vi setenv.sh (原来没有这个文件,需要创建出来) 添加 #add tomcat pid CATALINA_PID="$CATALINA_B ...