485:最大连续 1 的个数
This commit is contained in:
parent
e5b6dea383
commit
a91b04edf1
60
src/main/java/leetcode/editor/cn/MaxConsecutiveOnes.java
Normal file
60
src/main/java/leetcode/editor/cn/MaxConsecutiveOnes.java
Normal file
@ -0,0 +1,60 @@
|
||||
//给定一个二进制数组 nums , 计算其中最大连续 1 的个数。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
//
|
||||
//输入:nums = [1,1,0,1,1,1]
|
||||
//输出:3
|
||||
//解释:开头的两位和最后的三位都是连续 1 ,所以最大连续 1 的个数是 3.
|
||||
//
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
//
|
||||
//输入:nums = [1,0,1,1,0,1]
|
||||
//输出:2
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// 1 <= nums.length <= 10⁵
|
||||
// nums[i] 不是 0 就是 1.
|
||||
//
|
||||
// Related Topics 数组 👍 306 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
//485:最大连续 1 的个数
|
||||
public class MaxConsecutiveOnes {
|
||||
public static void main(String[] args) {
|
||||
Solution solution = new MaxConsecutiveOnes().new Solution();
|
||||
|
||||
}
|
||||
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int findMaxConsecutiveOnes(int[] nums) {
|
||||
int max = 0;
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
if (nums[i] == 0) {
|
||||
continue;
|
||||
}
|
||||
int temp = 1;
|
||||
i++;
|
||||
while (i< nums.length&&nums[i]==1){
|
||||
temp++;
|
||||
i++;
|
||||
}
|
||||
max = Math.max(temp,max);
|
||||
}
|
||||
return max;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
<p>给定一个二进制数组 <code>nums</code> , 计算其中最大连续 <code>1</code> 的个数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,1,0,1,1,1]
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>开头的两位和最后的三位都是连续 1 ,所以最大连续 1 的个数是 3.
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [1,0,1,1,0,1]
|
||||
<b>输出:</b>2
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>nums[i]</code> 不是 <code>0</code> 就是 <code>1</code>.</li>
|
||||
</ul>
|
||||
<div><div>Related Topics</div><div><li>数组</li></div></div><br><div><li>👍 306</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user