[leetcode-605-Can Place Flowers]
Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, flowers cannot be planted in adjacent plots - they would compete for water and both would die.
Given a flowerbed (represented as an array containing 0 and 1, where 0 means empty and 1 means not empty), and a number n, return if n new flowers can be planted in it without violating the no-adjacent-flowers rule.
Example 1:
Input: flowerbed = [1,0,0,0,1], n = 1
Output: True
Example 2:
Input: flowerbed = [1,0,0,0,1], n = 2
Output: False
Note:
- The input array won't violate no-adjacent-flowers rule.
- The input array size is in the range of [1, 20000].
- n is a non-negative integer which won't exceed the input array size.
思路:
看是否有连续三个0,注意最左边边界和最右边边界。
bool canPlaceFlowers(vector<int>& flowerbed, int n)
{
int len = flowerbed.size();
if(len== && flowerbed[]== && n<=)return true; int can =;
for(int i=;i<len;i++)
{
if(flowerbed[i]== && i->= && i+<len && flowerbed[i-]==&& flowerbed[i+]==)
{
can++;
flowerbed[i]=;
}
if(i==&& flowerbed[] == && flowerbed[]==)
{
can++;
flowerbed[]=;
}
if(i==len-&& flowerbed[len-] == && flowerbed[len-]==)
{
can++;
flowerbed[len-]=;
}
}
if(can>=n)return true;
return false;
}
[leetcode-605-Can Place Flowers]的更多相关文章
- LeetCode 605. Can Place Flowers (可以种花)
Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, ...
- LeetCode 605. 种花问题(Can Place Flowers) 6
605. 种花问题 605. Can Place Flowers 题目描述 假设你有一个很长的花坛,一部分地块种植了花,另一部分却没有.可是,花卉不能种植在相邻的地块上,它们会争夺水源,两者都会死去. ...
- 605. Can Place Flowers【easy】
605. Can Place Flowers[easy] Suppose you have a long flowerbed in which some of the plots are plante ...
- 【Leetcode_easy】605. Can Place Flowers
problem 605. Can Place Flowers 题意: solution1: 先通过简单的例子(比如000)发现,通过计算连续0的个数,然后直接算出能放花的个数,就必须要对边界进行处理, ...
- 【LeetCode】605. Can Place Flowers 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 贪婪算法 日期 题目地址:https://leetcode.c ...
- 605. Can Place Flowers种花问题【leetcode】
Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, ...
- 【Leetcode】605. Can Place Flowers
Description Suppose you have a long flowerbed in which some of the plots are planted and some are no ...
- 605. Can Place Flowers
Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, ...
- [LeetCode] 605. Can Place Flowers_Easy
Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, ...
- 605. Can Place Flowers零一间隔种花
[抄题]: Suppose you have a long flowerbed in which some of the plots are planted and some are not. How ...
随机推荐
- 【SoDiaoEditor电子病历编辑器】阶段性更新啦
转眼距离上一次v2正式发布已经过去一个半月了.github期间不定期push了二十几次,同时感谢分布在广州.福建.上海.北京的一众小伙伴,正是你们给出的建议,才让SoDiaoEditor不断完善. 我 ...
- Fiddler插件 --- 解密Elong Mapi请求参数及响应内容
当前问题: 在我们日常的Web/App测试过程中, Fiddler是一大辅助利器:在我们团队,也经常使用Fiddler进行App抓包测试. 艺龙 App使用的REST(内部称为Mapi)接口,在使用过 ...
- Java Garbage Collectors
Generational Collectors (分代收集器) GC algos optimised based on two hypotheses / observations: Most obje ...
- LindAgile.Modules模块化的设计
在LindAgile中有一个比较主推的技术,就是模块化,一切组件都可以被抽象成一个小小的模块,而每个小模块的实现可能又有多种方式,如日志模块可以有LindLoger,Log4net等实现,而具体在程序 ...
- [转]ef使用dbfirst方式连接mysql
为了学习ORM,选择了EntityFramework,经历了三天两夜的煎熬,N多次错误,在群里高手的帮助下,终于成功,现在将我的心路历程记录下来,一是让自己有个记录,另外就是让其它人少走些弯路. 我的 ...
- CocoaPods 安装使用教程
CocoaPods 是比较好的第三方类库管理工具.可通过 terminal 命令进行第三方类库的安装,非常方便. 安装: Mac terminal 输入:gem install cocoapods 若 ...
- .net开源权限管理系统
有业务请加QQ 245747009 源码地址:http://git.oschina.net/sunzewei/EIP 一.更新记录1.更新日期:2017-02-24 00:00:002.更新内容: 版 ...
- 织梦dedecms列表页dede:pagelist分页问题
pagelist是dede定义的一个分页标签.有时直接引用这个标签的时候,会出现分页标签变形问题.我在使用非默认模板的时候就遇到过两次. pagelist本身就有一些样式是在include/arc.l ...
- Java之进程与线程练习
1.设计一个线程类:创建3个子线程,每个线程分别打印数字,分别睡眠100,200,300ms ->每个执行都是20次 代码: package Homework; //1.设计一个线程类:创建3个 ...
- opencv 删除二值化图像中面积较小的连通域
对于上图的二值化图像,要去除左下角和右上角的噪点,方法:使用opencv去掉黑色面积较小的连通域. 代码 CvSeq* contour = NULL; double minarea = 100.0; ...