这题最关键的是处理最开始连续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. 消耗资源的SQL的定位方法;

    解答:select sql_text from v$sql where disk_reads > 1000 or (executions > 0 and buffer_gets/execu ...

  2. 【BZOJ】1632: [Usaco2007 Feb]Lilypad Pond(bfs)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1632 我简直是个sb... ... bfs都不会写.. 算方案还用2个bfs! 都不会整合到一个! ...

  3. linux程序设计——主机字节序和网络字节序(第十五章)

    15.2.10    主机字节序和网络字节序 当在基于intel处理器的linux机器上执行新版本号的server和客户程序时,能够用netstat命令查看网络连接状况.它显示了客户/server连接 ...

  4. ubuntu text mode和图形界面切换

    Ctrl+Alt+F1(或者F2~F6总共可以同时开6个text mode界面并行工作) Ctrl+Alt+F7切换到图形界面

  5. PHP手机号码归属地查询API接口

    淘宝网 API地址: http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=15850781443 参数: tel:手机号码 返回:JSON ...

  6. Python相对完美的URL拼接函数

    首先说下什么叫URL拼接,我们有这么一个HTML片段:   <a href="../../a.html">click me</a> 做为一只辛苦的爬虫,我们 ...

  7. asp.net 读取配置文件方法

    方法1: System.Collections.Specialized.NameValueCollection nvc = (System.Collections.Specialized.NameVa ...

  8. MathType编辑钢筋符号就是这么简单

    很多的用户在使用MathType公式编辑器的时候,发现它所包含的符号非常的多,几乎你在数学中看到的任何符号都能用MathType编辑出来.它能够满足各个学科对符号的需求,除了常规的数学物理符号之外,也 ...

  9. IntelliJ Idea Hide excluded folders 隐藏或显示你需要的文件夹

    IntelliJ Idea,以前用idea时,经常maven编译就出现了exclude下的文件夹通常是target,如何隐藏自己不想看见的文件夹,或显示偶尔会用到的文件夹 点击齿轮右下小标 选中文件夹 ...

  10. JZOJ.5257【NOIP2017模拟8.11】小X的佛光

    Description