Coding Exercise - Find Max Consecutive Ones 编程练习题 - 最多连续的 1

Coding Exercise - Find Max Consecutive Ones 编程练习题 - 最多连续的 1

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 0 and 1.
The length of input array is a positive integer and will not exceed 10,000

We count of the current consecutive 1s and the maximum count when iterating the numbers from left to right. If current element is 1, we increment the counter and compare to the maximum. Otherwise, we reset the current counter.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public:
int findMaxConsecutiveOnes(vector<int>& nums) {
auto r = 0, m1 = 0;
for (auto n: nums) {
if (n == 1) {
r ++;
m1 = max(m1, r);
} else {
r = 0;
}
}
return m1;
}
};

Using Python, this can be solved in a slightly different way - join the array as a string, split by delimiter ‘0’ and get the maximum length of the 1s.

1
return max([len(i) for i in "".join(map(str, nums)).split('0')])

Reposted to: https://helloacm.com/coding-exercise-find-max-consecutive-ones/

Support me and my work as a witness - witness thread by

  1. voting me here, or
  2. voting me as a proxy.

Thank you! Some of My Contributions: SteemIt Tutorials, Robots, Tools and APIs


题意:给定一个只含有1或0的数组,求连续为1最长长度是多少。

两个变量,记录当前遇到最长的长度m1,还有从上一次遇到0以来最长1的长度r,边遍历边更新。遇到0就重置r。

PYTHON的另一种做法:把数组连接成字符串,然后按0重新分组,求出剩下为1字符串的分组中最长的那个即可。

支持我的工作 支持我成为 见证人

  1. 请在 这里 投我一票, 或者
  2. 设置我 为代理.

谢谢您! 我的贡献:SteemIt 工具、API接口、机器人和教程

刚刚同步到我的博客

股东工具

  1. 成为股东:代理5 SP 即可 (退出股东 输入 0即可)
  2. 查询当前股东

请注意:每次代理都是以最后一次输入的SP数量为标准,比如已经代理10 SP,想多代理5 SP则需要输入 最后的数字 15 SP(而不是 5!)


This page is synchronized from the post: ‘Coding Exercise - Find Max Consecutive Ones 编程练习题 - 最多连续的 1’

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×