本文共 749 字,大约阅读时间需要 2 分钟。
给定一个二进制数组, 计算其中最大连续1的个数。
示例 1:
输入: [1,1,0,1,1,1]输出: 3解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3.
注意:
0
和1
。解答:
/** * 思路: * for循环遍历,判断是否为1,如果为1,count++ * 否则令count = 0,重新计数 * 判断count是否大于max,如果大于将其赋值给max * @param nums * @return */public static int findMaxConsecutiveOnes1(int[] nums) { int count = 0,max = 0; for (int i = 0; i < nums.length; i++) { if (nums[i] == 1){ count++; }else{ count = 0; } max = Math.max(count,max); } return max;}/** * 方法二: * @param nums * @return */public static int findMaxConsecutiveOnes2(int[] nums){ int maxHere = 0, max = 0; for (int num : nums) max = Math.max(max, maxHere = num == 0 ? 0 : maxHere + 1); return max;}
转载地址:http://bxslf.baihongyu.com/