problem

605. Can Place Flowers

题意:

solution1:

先通过简单的例子(比如000)发现,通过计算连续0的个数,然后直接算出能放花的个数,就必须要对边界进行处理,处理方法是如果首位置是0,那么前面再加上个0,如果末位置是0,就在最后面再加上个0。这样处理之后就默认连续0的左右两边都是1了,这样如果有k个连续0,那么就可以通过(k-1)/2来快速计算出能放的花的数量。

class Solution {
public:
bool canPlaceFlowers(vector<int>& flowerbed, int n) {
if(flowerbed.empty()) return false;
//if(flowerbed.front()==0) flowerbed.insert(flowerbed.begin(), 0);
if(flowerbed[]==) flowerbed.insert(flowerbed.begin(), );
//if(flowerbed.back()==0) flowerbed.insert(flowerbed.end(), 0);
if(flowerbed.back()==) flowerbed.push_back();
int sum = , cnt = ;
int len = flowerbed.size();
for(int i=; i<=len; i++)//err.
{
if(i<len && flowerbed[i]==) cnt++;//err.
else//need to compute sum when end with 0.
{
sum += (cnt-)/;
cnt = ;
}
}
return sum >= n;
}
};

参考

1. Leetcode_easy_605. Can Place Flowers;

2. Grandyang;

【Leetcode_easy】605. Can Place Flowers的更多相关文章

  1. 【Leetcode】605. Can Place Flowers

    Description Suppose you have a long flowerbed in which some of the plots are planted and some are no ...

  2. 【LeetCode】605. Can Place Flowers 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 贪婪算法 日期 题目地址:https://leetcode.c ...

  3. 【Leetcode_easy】1021. Remove Outermost Parentheses

    problem 1021. Remove Outermost Parentheses 参考 1. Leetcode_easy_1021. Remove Outermost Parentheses; 完

  4. 【Leetcode_easy】1022. Sum of Root To Leaf Binary Numbers

    problem 1022. Sum of Root To Leaf Binary Numbers 参考 1. Leetcode_easy_1022. Sum of Root To Leaf Binar ...

  5. 【Leetcode_easy】1025. Divisor Game

    problem 1025. Divisor Game 参考 1. Leetcode_easy_1025. Divisor Game; 完

  6. 【Leetcode_easy】1029. Two City Scheduling

    problem 1029. Two City Scheduling 参考 1. Leetcode_easy_1029. Two City Scheduling; 完

  7. 【Leetcode_easy】1030. Matrix Cells in Distance Order

    problem 1030. Matrix Cells in Distance Order 参考 1. Leetcode_easy_1030. Matrix Cells in Distance Orde ...

  8. 【Leetcode_easy】1033. Moving Stones Until Consecutive

    problem 1033. Moving Stones Until Consecutive 参考 1. Leetcode_easy_1033. Moving Stones Until Consecut ...

  9. 【Leetcode_easy】1037. Valid Boomerang

    problem 1037. Valid Boomerang 参考 1. Leetcode_easy_1037. Valid Boomerang; 完

随机推荐

  1. 文件操作中file.seek()方法

    摘要: file.seek()可以将文件游标移动到文件的任意位置,本文具体的file.seek()文件游标移动操作方法. file.seek()方法标准格式是:seek(offset,whence=0 ...

  2. HttpServletRequest获取浏览器、服务端和客户端信息

    如何通过HttpServletRequest来获取到上面的属性呢? 1.引入开源工具 <!-- https://mvnrepository.com/artifact/eu.bitwalker/U ...

  3. js的基础

    js:javascript的简写,是一种脚本语言. js的引入方式: 外部样式:<script src=""></script> 内部样式:<scri ...

  4. 搭建自己的博客(八):使用fontawesome框架来添加图标以及美化详情页

    在网页中有时候会使用到图标,自己弄图标有些麻烦所以就使用了fontawesome框架. 官网:   下载地址 我使用的fontawesome版本是5.5.0版本 1.先上变化的部分

  5. linux系列(十四):head命令

    1.命令格式: head [参数] [文件] 2.命令功能: head 用来显示档案的开头至标准输出中,默认head命令打印其相应文件的开头10行. 3.命令参数: -q 隐藏文件名 -v 显示文件名 ...

  6. windows gogs 安装

    windows 安装gogs: 1. 先下载 gogs ,直接解压.不用安装 https://dl.gogs.io/0.11.86/gogs_0.11.86_windows_amd64_mws.zip ...

  7. Storage Port Drivers

    为了学习存储知识,我也是拼了,来,翻译一下下面这篇微软的文章(如果有谁翻译过了,或者微软有中文翻译,请绕路): Storage Port Drivers Last Updated: 4/20/2017 ...

  8. python eval的用法

    >>>x = >>> eval( '3 * x' ) >>> eval('pow(2,2)') >>> eval('2 + 2' ...

  9. BZOJ1211树的计数

    裸的prufer结论. 给个小链接prufer序列 ,里面有一个性质4就是本题答案,严谨证明可以上网找一找,如果从多组组合角度理解也可以. 剩下的就是特判,n==1时,du==0,1个,du!=0,废 ...

  10. 手写实现RPC框架(不带注册中心和带注册中心两种)

    实现自己的RPC框架如果不需要自定义协议的话那就要基于Socket+序列化. ProcessorHandler:主要是用来处理客户端的请求. package dgb.nospring.myrpc; i ...