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. 003---hibernate主要接口介绍

    Hibernate可以访问JNDI.JDBC.JTA JNDI(Java名称和目录接口):主要管理我们对象,特别是EJB应用,它会把所有EJB应用加入到JNDI这棵树上,Tomcat连接池也是把对象注 ...

  2. SpringMVC中使用RedirectAttributes重定向传参,防止暴露参数

    RedirectAttributes是SpringMVC3.1版本之后出来的一个功能,专门用于重定向之后还能带参数跳转的. 当我从jsp页面函数中带参数到controller层方法,方法执行完毕后返回 ...

  3. vue组件之间的通信以及如何在父组件中调用子组件的方法和属性

    在Vue中组件实例之间的作用域是孤立的,以为不能直接在子组件上引用父组件的数据,同时父组件也不能直接使用子组件的数据 一.父组件利用props往子组件传输数据 父组件: <div> < ...

  4. countDownLatch和cyclicBarrier

    < Effecit In Java >说过,从java 1.5发现版本开始, 就不建议使用wait和notify,它们使用比较困难,可以使用更高级并发工具来替代. 图一所说的同步器是指那些 ...

  5. IIS无法启动,应用程序池自动关闭,应用程序池XXXX将被自动禁用 解决方案之一

    最近,新任职的公司有一台测试服务(Windows Server 2008 R2 + IIS6.1)器因突然停电,造成了意外“损伤”.来电后再次开机,发现IIS里大部分的网站均打不开.均为如下(图01) ...

  6. markdown 基础语法

    markdown 基础 你好,我是markdown文档 介绍 Markdown是一种可以使用普通文本编辑器编写的标记语言,通过简单的标记语法,它可以使普通文本内容具有一定的格式. 说的简单一点,mar ...

  7. 基于谱聚类的三维网格分割算法(Spectral Clustering)

    谱聚类(Spectral Clustering)是一种广泛使用的数据聚类算法,[Liu et al. 2004]基于谱聚类算法首次提出了一种三维网格分割方法.该方法首先构建一个相似矩阵用于记录网格上相 ...

  8. javascript痛点之二作用域链

    1.执行环境(执行上下文) 先看段代码 var a = 10; var b = 20; function cc(){ var c = 30; alert("b="+b); } cc ...

  9. 10分钟弄懂javascript数组

    建议阅读时间 : 10分钟 主要内容:javascript数组的基本概念.属性.方法 新建数组: var arr01 = ["a","b","c&qu ...

  10. python3 简单实现从csv文件中读取内容,并对内容进行分类统计

    新手python刚刚上路,在实际工作中遇到如题所示的问题,尝试使用python3简单实现如下,欢迎高手前来优化import csv #打开文件,用with打开可以不用去特意关闭file了,python ...