GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — [LeetCode] 485. Max Consecutive Ones · Issue #485 · grandyang/leetcode Open Description
Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Note:
这道题让求最大连续1的个数,不是一道难题。可以遍历一遍数组,用一个计数器 cnt 来统计1的个数,方法是如果当前数字为0,那么 cnt 重置为0,如果不是0,cnt 自增1,然后每次更新结果 res 即可,参见代码如下:
解法一:
由于是个二进制数组,所以数组中的数字只能是0或1,那么连续1的和跟个数相等,所以可以计算和,通过加上 num,再乘以 num 来计算,如果当前数字是0,那么 sum 就被重置为0,还是要更新结果 res,参见代码如下:
解法二:
Github 同步地址:
类似题目:
参考资料: https://leetcode.com/problems/max-consecutive-ones/
ActivityMetadataMetadataAssigneesLabelsNo labels TypeProjectsMilestoneRelationshipsDevelopmentIssue actions |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |