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 ...
随机推荐
- ubuntu 14.04 中安装R和Rstudio
1. 安装R 1.1 首先添加镜像源 sudo gedit /etc/apt/sources.list # 加入新镜像源: deb http://cran.rstudio.com/bin/linux/ ...
- 【vijos】1746 小D的旅行(dijkstra)
https://vijos.org/p/1746 这题就是水题.裸的跑完每个点的最短路后直接可以暴力出解.. 这题贴出来是因为我改了下我的dijkstra的模板...(其实是原来一直写错了233 注意 ...
- CFontDialog学习
void CMfcFontDlgDlg::OnBtnFont() { // Show the font dialog with all the default settings. CFontDialo ...
- android去权限反编译,签名,zipalign优化
反编译:上工具ApkTool 下载自行搜索google apktool github cd apktool目录 java -jar apktool_2.0.1.jar d xx.apk 生成xx目录 ...
- 优化phpstorm运行卡顿问题
在PHPSTORM中点击导航菜单:Help -> Edit Custom VM Options 如果是第一次点击,会提示是否新建配置文件,点击“yse” 在弹出的编辑框末尾加上以下配置 -Daw ...
- ObjC利用正则表达式抓取网页内容(网络爬虫)
本文转载至 http://www.cocoachina.com/bbs/read.php?tid=103813&fpage=63 在开发项目的过程,很多情况下我们需要利用互联网上的一些数据,在 ...
- ZOJ 2676 Network Wars[01分数规划]
ZOJ Problem Set - 2676 Network Wars Time Limit: 5 Seconds Memory Limit: 32768 KB Special J ...
- 一个有意思的 Java HashSet 问题
昨天,在百度的 java吧 看到有人问关于 HashSet 的问题.下面是他贴出的代码: import java.util.HashSet; public class JavaTest { publi ...
- HDU 1863 畅通工程(Kruskal)
畅通工程 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- php自定义函数: 下载本地服务器的大文件
// 使用方法 $file_path = './a.zip'; // 只能是本地服务器文件, 多大的文件都支持!! down_file($file_path); // 函数参数: 服务器文件路径,下载 ...