leetcode解题之485. Max Consecutive Ones Java版 (二进制连续最长1的个数)
来源:互联网 发布:swatch黑白时分淘宝 编辑:程序博客网 时间:2024/05/22 02:20
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: 3Explanation: 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
0
and1
. - The length of input array is a positive integer and will not exceed 10,000
public class Solution {public int findMaxConsecutiveOnes(int[] nums) {int result = 0;int temp = 0;for (int i = 0; i < nums.length; i++) {if (nums[i] == 0)temp = 0;else {temp += 1;result = Math.max(temp, result);}}return result;}// 动态规划public int findMaxConsecutiveOnes(int[] nums) {// 才用逆向思维,数组长度要加1int dp[] = new int[nums.length + 1];int max = 0;for (int i = nums.length - 1; i >= 0; i--)if (nums[i] == 1) {dp[i] = dp[i + 1] + 1;max = Math.max(dp[i], max);}return max;}// 动态规划public int findMaxConsecutiveOnes(int[] nums) {int dp[] = new int[nums.length];int max = 0;for (int i = 0; i < nums.length; i++)if (nums[i] == 1) {if (i == 0)dp[i] = 1;elsedp[i] = dp[i - 1] + 1;max = Math.max(dp[i], max);}return max;}}
0 0
- leetcode解题之485. Max Consecutive Ones Java版 (二进制连续最长1的个数)
- [LeetCode] Max Consecutive Ones 最大的“1”连续个数
- LeetCode 485 Max Consecutive Ones 最大连续1的个数
- Leetcode 485. Max Consecutive Ones 最大连续1 解题报告
- 485. Max Consecutive Ones 找二进制数中连续1
- LeetCode 485:Max Consecutive Ones(连续1的最大个数,边界条件)
- 【LeetCode】485.Max Consecutive Ones解题报告
- [Leetcode] 485. Max Consecutive Ones 解题报告
- [LeetCode]485. Max Consecutive Ones(最大连续为1的数量)
- LeetCode之路:485. Max Consecutive Ones
- leetcode oj java 485. Max Consecutive Ones
- LeetCode 485. Max Consecutive Ones(Java)
- LeetCode之Max Consecutive Ones
- leetcode Max Consecutive Ones 最大连续长度
- 【LeetCode】Max Consecutive Ones 解题报告
- LeetCode 485. Max Consecutive Ones
- 【Leetcode】485. Max Consecutive Ones
- LeetCode 485. Max Consecutive Ones
- BZOJ3809 Gty的二逼妹子序列
- Linux 下 find 查找命令详解 && 黏滞位
- LeetCode002 Add Two Numbers
- NB-IoT通信模组/模块
- 工具之滚动截图
- leetcode解题之485. Max Consecutive Ones Java版 (二进制连续最长1的个数)
- 剑指Offer5:从尾到头打印链表
- 递推递归练习 E 养兔子
- ssh相关使用命令
- 最优布线问题(克鲁斯卡尔)
- 递推递归练习 F 计算组合数
- MySql5.7在Windows系统中的安装、修改root密码、添加到系统服务
- python文件操作
- 全自动修改模式说明