Solution:
It is an easy question. If element is 1, count+1; otherwise, count = 0. Update res for each loop.
Code:
class Solution { public: int findMaxConsecutiveOnes(vector<int>& nums) { int res = 0, l = nums.size(); for(int i=0; i<l; ++i){ int t = i; while(t<l && nums[t] == 1) ++t; res = max(res, t-i); i = t; } return res; } };Another way to code:
class Solution { public: int findMaxConsecutiveOnes(vector<int>& nums) { int res = 0, l = nums.size(), c1 = 0;//c1 is the counting of 1; for(int i=0; i<l; ++i){ ++c1; if(nums[i] == 0) c1 = 0; res = max(res, c1); } return res; } };
No comments :
Post a Comment