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. 最新的chart 聊天功能( webpack2 + react + router + redux + scss + nodejs + express + mysql + es6/7)

    请表明转载链接: 我是一个喜欢捣腾的人,没事总喜欢学点新东西,可能现在用不到,但是不保证下一刻用不到. 我一直从事的是依赖angular.js 的web开发,但是我怎么能一直用它呢?看看最近火的一塌糊 ...

  2. 调试 Dockerfile - 每天5分钟玩转 Docker 容器技术(15)

    包括 Dockerfile 在内的任何脚本和程序都会出错.有错并不可怕,但必须有办法排查,所以本节讨论如何 debug Dockerfile. 先回顾一下通过 Dockerfile 构建镜像的过程: ...

  3. MyBatis之TypeHandler

    在大学写web应用的时候经常会遇到这么个问题,当我要插入一条数据,某个数据是Date类型,数据库中却是VARCHAR类型,这个时候可能会傻乎乎的先把这个数据自己手动转换成String类型再插入到数据库 ...

  4. AMD及requireJS

    前面的话 由CommonJS组织提出了许多新的JavaScript架构方案和标准,希望能为前端开发提供统一的指引.AMD规范就是其中比较著名一个,全称是Asynchronous Module Defi ...

  5. Vue中comoputed中的数据绑定

    Vue中的数据实现响应式绑定是在初始化的时候利用definePrototype的定义set和get过滤器,在进行组件模板编译时实现water的监听搜集依赖项,当数据发生变化时在set中通过调用dep. ...

  6. Hibernate createQuery调用joincolumn

    1. Beans a. Version Bean package locationService.beans; import java.sql.Timestamp; import java.util. ...

  7. let与const详解

    在ES6中,js首次引入了块级作用域的概念,而什么是块级作用域? 众所就知,在js当中存在预解析的概念,就是变量提升.并且只存在全局作用域和私有作用域.在全局定义的变量就是全局变量,而在函数内部定义的 ...

  8. 关于DB2死锁处理

    昨天在修改字段类型的时候引起了死锁,我一般会使用REORG命令处理,在控制中心下 只需要执行 REORG TABLE TABLENAME  命令就可以, 但是在 RazorSQL 下执行此命令会报缺失 ...

  9. 《算法4》1.5 - Union-Find 算法解决动态连通性问题,Python实现

    Union-Find 算法(中文称并查集算法)是解决动态连通性(Dynamic Conectivity)问题的一种算法,作者以此为实例,讲述了如何分析和改进算法,本节涉及三个算法实现,分别是Quick ...

  10. 【Netty】Netty之Bootstrapping

    一.前言 前面已经学习了Netty的EventLoop以及线程模型,接着学习Netty的Bootstrapping. 二.Bootstrapping 在学习了Netty中的很多组件后,如何将这些组件有 ...