485. Max Consecutive Ones【easy】
485. Max Consecutive Ones【easy】
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
0and1. - The length of input array is a positive integer and will not exceed 10,000
解法一:
class Solution {
public:
int findMaxConsecutiveOnes(vector<int>& nums) {
int max = ;
int temp = ;
for (int i = ; i < nums.size(); ++i)
{
if (nums[i] == )
{
temp++;
max = temp > max ? temp : max;
}
else
{
temp = ;
}
}
return max;
}
};
思路很简单:是1就累加并且判断是否需要更新max,不是1就把累加和归为0,继续遍历。
解法二:
public int findMaxConsecutiveOnes(int[] nums) {
int maxHere = , max = ;
for (int n : nums)
max = Math.max(max, maxHere = n == ? : maxHere + );
return max;
}
大神解释如下:
The idea is to reset maxHere to 0 if we see 0, otherwise increase maxHere by 1
The max of all maxHere is the solution
110111
^ maxHere = 1
110111
.^ maxHere = 2
110111
..^ maxHere = 0
110111
...^ maxHere = 1
110111
....^ maxHere = 2
110111
.....^ maxHere = 3
解法三:
int findMaxConsecutiveOnes(int* nums, int numsSize) {
int max = ;
int sum = ;
for (int i=; i<numsSize; i++)
{
sum = (sum+nums[i])*nums[i];
if(max<sum){max=sum;}
}
return max;
}
这方法更牛逼,大神解释如下:Use the fact that multiplication with 0 resets everything..
485. Max Consecutive Ones【easy】的更多相关文章
- 479. Second Max of Array【easy】
Find the second max number in a given array. Notice You can assume the array contains at least two n ...
- 【leetcode】485. Max Consecutive Ones
problem 485. Max Consecutive Ones solution1: class Solution { public: int findMaxConsecutiveOnes(vec ...
- 121. Best Time to Buy and Sell Stock【easy】
121. Best Time to Buy and Sell Stock[easy] Say you have an array for which the ith element is the pr ...
- 605. Can Place Flowers【easy】
605. Can Place Flowers[easy] Suppose you have a long flowerbed in which some of the plots are plante ...
- 561. Array Partition I【easy】
561. Array Partition I[easy] Given an array of 2n integers, your task is to group these integers int ...
- 217. Contains Duplicate【easy】
217. Contains Duplicate[easy] Given an array of integers, find if the array contains any duplicates. ...
- 521. Longest Uncommon Subsequence I【easy】
521. Longest Uncommon Subsequence I[easy] Given a group of two strings, you need to find the longest ...
- 170. Two Sum III - Data structure design【easy】
170. Two Sum III - Data structure design[easy] Design and implement a TwoSum class. It should suppor ...
- 160. Intersection of Two Linked Lists【easy】
160. Intersection of Two Linked Lists[easy] Write a program to find the node at which the intersecti ...
随机推荐
- FireDac Pooling
1.建立FDManager的ConnectionDef.并设置此Pooling为True. 2.建立Thread类进行多个FDConnection连接DB. 3.本列是oracle远程数据.如下图: ...
- SpingMVC实现操作的第一方式
(一) 使用SpringMVC框架的步骤 (1):在web.xml中配置前端控制器 (2):在Spring-servlet.xml中配置 配置处理器映射器HandlerMapping(处理器Handl ...
- OpenDigg - 挖掘优质开源项目库
OpenDigg - 挖掘优质开源项目库 OpenDigg专注于挖掘优质的开源项目库,通过技术和人工将软件项目分类整理,同时辅助简要的编译,方便广大程序员便捷地找到需要的开源项目. OpenDigg刚 ...
- 科研不是比赛,而是一种对未知和完美的自我追求——跟邢波(Eric Xing)面对面聊科研
编者按:6月26日,2014年国际机器学习大会(ICML)在北京国际会议中心完美落幕.作为机器学习领域两大顶尖年会之一,这是 ICML大会30多年来首次来到中国和远东,在国内的机器学习界震动不小.身为 ...
- 在Linux中切换用户时变成-bash-4.3$
增加普通用户 [root@git-node1 ~]#useradd nulige [root@git-node1 ~]#passwd nulige 输入两次密码 [root@git-node1 ~]# ...
- openstack学习笔记(一)-openstack的基础知识
一.OpenStack的基础知识 openstack是一个由NASA(美国国家航空航天局)和Rackspace合作研发并发起的,以Apache2.0许可证(兼容GPLv3以及DFSG)授权的自由软件和 ...
- Oracle架构全图
- RequireJS全面讲解
异步模块定义(AMD) 谈起RequireJS,你无法绕过提及JavaScript模块是什么,以及AMD是什么. JavaScript模块只是遵循SRP(Single Responsibility ...
- git 出现502错误后用depth一步一步来
公司有个项目的git仓库,因为一些二进制文件也放在里面,版本迭代后,整个仓库特别大,有好几G. 直接git clone是不行的,会报这样的错误: error: RPC failed; HTTP 502 ...
- 修改 Ubuntu 13.04 LAMP 服务器端口号
因为今天想让一台Ubuntu 13.04服务器对外 web 服务的端口号为8000,自己改了一下,但是就是无法访问,端口后依然为 80.所以在网上找了一下修改端口的办法,原来我还少修改了一个文件,这里 ...