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:

  1. The input array won't violate no-adjacent-flowers rule.
  2. The input array size is in the range of [1, 20000].
  3. 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]的更多相关文章

  1. LeetCode 605. Can Place Flowers (可以种花)

    Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, ...

  2. LeetCode 605. 种花问题(Can Place Flowers) 6

    605. 种花问题 605. Can Place Flowers 题目描述 假设你有一个很长的花坛,一部分地块种植了花,另一部分却没有.可是,花卉不能种植在相邻的地块上,它们会争夺水源,两者都会死去. ...

  3. 605. Can Place Flowers【easy】

    605. Can Place Flowers[easy] Suppose you have a long flowerbed in which some of the plots are plante ...

  4. 【Leetcode_easy】605. Can Place Flowers

    problem 605. Can Place Flowers 题意: solution1: 先通过简单的例子(比如000)发现,通过计算连续0的个数,然后直接算出能放花的个数,就必须要对边界进行处理, ...

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

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

  6. 605. Can Place Flowers种花问题【leetcode】

    Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, ...

  7. 【Leetcode】605. Can Place Flowers

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

  8. 605. Can Place Flowers

    Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, ...

  9. [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, ...

  10. 605. Can Place Flowers零一间隔种花

    [抄题]: Suppose you have a long flowerbed in which some of the plots are planted and some are not. How ...

随机推荐

  1. Microsoft Windows 2003 SP2 - 'ERRATICGOPHER' SMB Remote Code Execution

    EDB-ID: 41929 Author: vportal Published: 2017-04-25 CVE: N/A Type: Remote Platform: Windows Aliases: ...

  2. UEditor上传图片到七牛C#(后端实现)

    由于个人网站空间存储有所以选择将图片统一存储到七牛上,理由很简单 1  免费10G 的容量  ,对个人网站足够用 2  规范的开发者文档 和完善的sdk(几乎所有热门语言sdk) 整体思路 图片上传七 ...

  3. php中for循环的应用

    for 循环是 PHP 中最复杂的循环结构.它的行为和 C 语言的相似.在PHP中使用的是执行相同的代码集的次数. for 循环的语法是: for (expr1; expr2; expr3)state ...

  4. Plupload上传插件自定义图片的修改

    若自定义的一个上传图片效果,代码(可能不全),当用户再次点击所有或任意一个上传图片的input时,uploader.files已经多了客户再次上传的图片,但是你就想要最后的两张图片,这就可以使用到up ...

  5. 基于Redis实现分布式锁(1)

    转自:http://blog.csdn.net/ugg/article/details/41894947 背景在很多互联网产品应用中,有些场景需要加锁处理,比如:秒杀,全局递增ID,楼层生成等等.大部 ...

  6. poi解析excel

    一.遇见的问题: 当单元格设置为日期类型时,cell.getCellStyle().getDataFormat()返回的值都为176. poi jar包3.14以上不支持用cell.getCellTy ...

  7. set集合(一)

    set 叫做集合  作用:  去重(去除重复) 举例-- >>> name_list = ['alben','nichole','lucy','andy','tom','alben' ...

  8. angular实现的文字上下无缝滚动

    最近在学习angularJs,业余时间随便写了一个文字上下无缝滚动的例子,主要写了一个小小的指令. css代码:主要控制样式 <style type="text/css"&g ...

  9. pick off your glasses

    我一直在想,为什么带眼镜时间长了机不愿意再摘下来呢,或者说摘下来感觉很不舒服.当然了,这更多的是内心里的一种感觉而已. 其实,我突然认为这是一种不自信,在这样一个物欲横流的社会中,当你眼前模模糊糊,而 ...

  10. laravel 服务容器实现原理

    前言 通过实现laravel 框架功能,以便深入理解laravel框架的先进思想. 什么是服务容器 服务容器是用来管理类依赖与运行依赖注入的工具.Laravel框架中就是使用服务容器来实现 ** 控制 ...