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
0
and1
. - 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 ...
随机推荐
- [BZOJ3551][ONTAK2010]Peaks(加强版)(Kruskal重构树,主席树)
3551: [ONTAK2010]Peaks加强版 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 2438 Solved: 763[Submit][ ...
- python基础之二
1. 数据类型 1.1 数字 数字的作用:与数字相关,例如:手机号.QQ号.身份证号等,用数字表示 数字分为:整数(int).浮点数(float).复数(了解) 例子: age = 10 print( ...
- @RequestParam注解的使用
自SpringMVC4.2之后,RequestParam内部有4个参数: 1.String name; 2.String value; 3.boolean required; 4.String def ...
- 【R笔记】order函数例子
问题: R中提供的例子不太懂,为什么得出的结果是6 5 2 1 7 4 10 8 3 9呢? (ii <- order(x <- c(1,1,3:1,1:4,3), y & ...
- Swift中的GCD——常见的dispatch方法
什么是GCD Grand Central Dispatch (GCD)是Apple开发的一个多核编程的解决方法.该方法在Mac OS X 10.6雪豹中首次推出,并随后被引入到了iOS4.0中.GCD ...
- delphi杀进程的两种方式
delphi杀进程的两种方式 uint unit Tlhelp32; 第一种:比较简单,根据标题,找到窗口,再找到进程,杀死进程 procedure KillProgram(WindowTitle : ...
- C# 7 新特性-2
在之前的C# 7 新特性博客中,我们谈到了Tuples,Record Type和Pattern Matching.这些都是C#新特性中最可能出现的.在本博客中,我们会提到更多的一些特性,虽然这些特性不 ...
- Python扫描指定文件夹下(包含子文件夹)的文件
扫描指定文件夹下的文件.或者匹配指定后缀和前缀的函数. 假设要扫描指定文件夹下的文件,包含子文件夹,调用scan_files("/export/home/test/") 假设要扫描 ...
- iOS: 格式化新浪微博/QQ说说等等的发布时间
介绍:对于一些社交工具,我们可以发布一些说说或者心情什么的,如新浪微博,QQ,微信等,发布成功后,上面都会有一个发布的时间. 这个时间并不是具体的NSDate类型,而是经过格式化过的符合一般标准的模式 ...
- Hadoop之Sqoop详解
sqoop数据迁移1.简介 sqoop是apache旗下一款“Hadoop和关系数据库服务器之间传送数据”的工具. 导入数据:MySQL,Oracle导入数据到Hadoop的HDFS.HIVE.HBA ...