算法练习笔记(一)


刚好上算法课的一学期

逐渐在这个blog更新打卡自己在leetcode上面做的各种类型的题和感想

会有各种各样的话唠和吐槽


------------------------充满善意的分界线-----------------------------------------

题目地址:https://leetcode.com/problems/max-consecutive-ones/?tab=Description

题目:MaxConsecutive Ones

分类:Array

难度:Easy

题目内容:

 

Given a binary array, find the maximum number of consecutive 1s in thisarray.

   1   

Example 1:

Input: [1,1,0,1,1,1]

Output: 3

Explanation: The first two digits or the last threedigits 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

刚开始肯定都是先试试简单的题嘛,发现各种简单粗暴

只需要寻找一个数组,定两个变量,一个用来计数,一个用来比较存储最大值最后输出就OK,刚开始还认为难点在输入上,然而发现温柔体贴的官方已经在提交界面为你量身定做了一个已经输入数组数值的vector容器(摊手),于是一切就变得越发简单粗暴了起来= =

class Solution {
public:
    int findMaxConsecutiveOnes(vector<int>& nums) {
    	int key = 0,count = 1;
    	int n = nums.size();
    	for(int i = 0; i < n; i ++){
    		if(nums[i] == 1){
    			for(int i1 = i + 1; i1 < n; i1 ++){
    				if(nums[i1] != nums[i]){
    					i = i1;
    					break;
    				}
    				count ++;	
    			}
    			if(count > key)
    			key = count;
    			count = 1;
    		}
    	}
    	return key;
    }
};

leetcode上提交代码是提交到一个solution的类里面,而且还不用自己输入输出,虽然有点复杂研究了很久,不过Ta开心就好

PS:

之前提交代码的时候,一直在编译错误报错:

expectedunqualified-idbeforestringconstant

完全不懂是啥好吗

然而经过我始终坚持不懈的努力,发现原来被自己删了一个'}',还有可能是少了一个‘;’

果然一段时间不打代码就会变傻,希望自己的智商能够早点在线

以上

愿每一个打代码的夜晚都能早睡:)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值