这题最关键的是处理最开始连续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的更多相关文章

  1. LeetCode——Max Consecutive Ones

    LeetCode--Max Consecutive Ones Question Given a binary array, find the maximum number of consecutive ...

  2. [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 ...

  3. LeetCode Max Consecutive Ones II

    原题链接在这里:https://leetcode.com/problems/max-consecutive-ones-ii/ 题目: Given a binary array, find the ma ...

  4. 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 ...

  5. [LeetCode] Max Consecutive Ones 最大连续1的个数

    Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1, ...

  6. LeetCode 1004. Max Consecutive Ones III

    原题链接在这里:https://leetcode.com/problems/max-consecutive-ones-iii/ 题目: Given an array A of 0s and 1s, w ...

  7. 【leetcode】485. Max Consecutive Ones

    problem 485. Max Consecutive Ones solution1: class Solution { public: int findMaxConsecutiveOnes(vec ...

  8. 485. Max Consecutive Ones - LeetCode

    Question 485. Max Consecutive Ones Solution 题目大意:给一个数组,取连续1的最大长度 思路:遍历数组,连续1就加1,取最大 Java实现: public i ...

  9. LeetCode——Longest Consecutive Sequence

    LeetCode--Longest Consecutive Sequence Question Given an unsorted array of integers, find the length ...

随机推荐

  1. QT creator 编辑多个UI 文件 出现 无法解析的外部符号的错误

    创建一般的Qt Gui 程序一般会默认一个UI 文件 ,但是随着应用程序窗口的增多,同时编辑多个UI 界面是必须的. 假设我们已经创建好了一个QTUI的工程,里面已经默认了一个UI文件,但是想在添几个 ...

  2. sql语句中3表删除和3表查询

    好久没来咱们博客园了,主要近期在忙一些七七八八的杂事,包括打羽毛球比赛的准备和自己在学jqgrid的迷茫.先不扯这些没用的了,希望大家能记得小弟,小弟在此谢过大家了. 回归正题:(以下的sql是本人在 ...

  3. (一)Spring容器相关操作

    一.spring事件 spring的事件有如下两个成员. 1.ApplicationEvent,容器事件,由容器发布 2.ApplicationListener 监听器,可以由容器中的任何监听器Bea ...

  4. Lumen 队列

    队列 简介 连接 Vs. 队列 驱动的必要设置 创建任务类 生成任务类 任务类结构 分发任务 延迟分发 任务链 自定义队列 & 连接 指定任务最大尝试次数 / 超时值 频率限制 错误处理 运行 ...

  5. SurvivalShooter学习笔记(四.敌人攻击)

    此案例中,敌人始终朝着玩家移动 到达攻击玩家范围时(身上的大的触发器被玩家触发时(敌人靠近玩家,当身上的触发器被触发,且对象是玩家时条件达成)) 隔一个时间端,打击玩家一下,对玩家造成伤害,玩家掉血, ...

  6. JavaScript------一元运算符+的使用

    var y = "5"; // y 是一个字符串 var x = + y; // x 是一个数字 var y = "John"; // y 是一个字符串 var ...

  7. iOS从当前隐藏导航界面push到下一个显示导航界面出现闪一下的问题

    本文转载至 http://blog.csdn.net/woaifen3344/article/details/41284319 navios 如果有朋友遇到从当前隐藏导航界面push到下一个显示导航界 ...

  8. iOS-地图开发 Plist文件设置权限

    解决办法: 在.Plist文件中添加 <key>NSLocationUsageDescription</key> <string>请点击“好”以允许访问. 若不允许 ...

  9. [转]廖雪峰Git教程总结

  10. ionicframework I ------------- 初体验

    ionicframework I -------------  初体验 Create hybrid mobile apps with the web technologies you love. Fr ...