[LC] 485. Max Consecutive Ones
Given a binary array, find the maximum number of consecutive 1s in this array.
Example 1:
Input: [1,1,0,1,1,1]
Output: 3
Explanation: The first two digits or the last three digits are consecutive 1s.
The maximum number of consecutive 1s is 3.
Note:
- The input array will only contain
0and1. - The length of input array is a positive integer and will not exceed 10,000
class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int res = 0, cur = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 1) {
if (i == 0 || nums[i - 1] == 1) {
cur += 1;
} else {
cur = 1;
}
// case [0, 1] res cover both cases
res = Math.max(res, cur);
}
}
return res;
}
}
[LC] 485. Max Consecutive Ones的更多相关文章
- 【leetcode】485. Max Consecutive Ones
problem 485. Max Consecutive Ones solution1: class Solution { public: int findMaxConsecutiveOnes(vec ...
- 485. Max Consecutive Ones【easy】
485. Max Consecutive Ones[easy] Given a binary array, find the maximum number of consecutive 1s in t ...
- 485. Max Consecutive Ones - LeetCode
Question 485. Max Consecutive Ones Solution 题目大意:给一个数组,取连续1的最大长度 思路:遍历数组,连续1就加1,取最大 Java实现: public i ...
- 485. Max Consecutive Ones最长的连续1的个数
[抄题]: Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Inpu ...
- 485. Max Consecutive Ones@python
Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1, ...
- 485. Max Consecutive Ones
题目 Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: ...
- 8. leetcode 485. Max Consecutive Ones
Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1, ...
- LeetCode 485. Max Consecutive Ones (最长连续1)
Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1, ...
- (双指针) leetcode 485. Max Consecutive Ones
Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1, ...
随机推荐
- TypeError: TF_SessionRun_wrapper: expected all values in input dict to be ndarray.
在用Embedding时出现了这个问题,具体的代码: model.add(Embedding(input_dim = vocab_size, output_dim = embedding_vector ...
- usr/sbin/inetd
root 4 0.0 1344 1204? S 17:09 0:10 /usr/sbin/inetd 运行 Internet 超级 服务器,它负责监听 Internet sockets 上的连接,并调 ...
- 一个或多个实体的验证失败。有关详细信息,请参阅“EntityValidationErrors”属性
自定义异常类:FormattedDbEntityValidationException public class FormattedDbEntityValidationException : Exce ...
- 相信301跳转大家都知道 rewrite
相信301跳转大家都知道,这样有利于权重集中,但是我在.htaccess文件写上: RewriteEngine on rewriteCond %{http_host} ^phpddt.com [NC] ...
- 在scala命令行中加入类库
在scala命令行中加入scala的类库. scala -toolcp $HOME/.ivy2/cache/org.scalanlp/breeze_2.12/jars/breeze_2.12-0.13 ...
- POJ-2561 Network Saboteur(DFS)
题目: A university network is composed of N computers. System administrators gathered information on t ...
- PAT Advanced 1023 Have Fun with Numbers (20) [⼤整数运算]
题目 Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, ...
- intellij idea安卓开发配置
1.java sdk 2.java ndk 3.gradle https://gradle.org/install/#manually 配置properties 删除根目录下android{} htt ...
- 网站的ssl证书即将过期,需要续费证书并更新
SSL这个证书的续费也挺奇怪,续费跟新购买一样. 证书这个东西,申请成功之后,每次都要重新下载,需要处理好格式之后,放在服务器的指定目录里. 大致操作如下: 首先,申请/续费证书,证书下来后,下载下来 ...
- lua 把图片转换成base64
调用实例 require("ZZBase64") local files local file = io.open("E:\\2342.jpg","r ...