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 ...
随机推荐
- C++ 检查Windows服务运行状态
检查Windows服务运行状态 C++ Code 1234567891011121314151617181920212223242526272829303132333435363738394041 ...
- 60、常规控件(3)Snackbar-可操作的提示框,Toast升级版
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app=&quo ...
- python 保存csv文件
利用pandas库, 将numpy的array数据保存成csv格式的文件: import pandas as pd import numpy as np data = pd.read_csv('C:\ ...
- [Algorithms] Graph Traversal (BFS and DFS)
Graph is an important data structure and has many important applications. Moreover, grach traversal ...
- String 转 List<Map<String, Object>>
public static List<Map<String, Object>> toListMap(String json){ List<Object> list ...
- Oracle 的安全保障 commit &checkpoint
Oracle 的安全 commit &checkpoint commit ---lgwr 事务相关的操作,保证事务的安全. commit标志着事务的结束.意味着别人对你事务操作的结果可见. c ...
- IE11 Enterprise Mode Template missing from GPMC
IE11 Enterprise Mode Template missing from GPMC Reason:You have not copied the new IE11 Enterpri ...
- VS2010 / MFC + OpenCV 2.4.1打开图片
Windows 7 x64,VS2010 / MFC + OpenCV 2.4.1打开图片显示到Picture控件中. OpenCV 2.2.OpenCV 2.3同样适用. 工具/原料 WinXP / ...
- IO流入门-第十一章-PrintStream_PrintWriter
DataInputStream和DataOutputStream基本用法和方法示例 /* java.io.PrintStream:标准的输出流,默认打印到控制台,以字节方式 java.io.Print ...
- java基础09 数组的使用
/** * 求数组中的最大值 */ @Test public void test14(){ //定义一个数组 参赛的选手 int [] nums={50,20,30,80,100,90}; //定义一 ...