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.

题目标签:Array

  题目给了我们一个 flowerbed array, 和一个 n, 让我们找出是否右足够的空间来置放 n 个花。 每一朵符合条件的花,它的左右相邻位置 不能有花。

  所以我们要遍历flowerbed array:

    如果遇到0的话,还要检查这个0的左右两边是否都是0,都是0的情况下,才可以算作一个空间来置放花,然后可以额外跳过右边的0;

    如果遇到1的话,只需要额外跳过1右边的位置;

    一旦当空间数量 已经 等于 n 了,就不需要继续找下去了,直接返回true。(这里要加一个 = ,因为给的 n 有可能是 0)

  当遍历完了,说明空间不够,返回 false 即可。

Java Solution:

Runtime beats 60.45%

完成日期:10/15/2017

关键词:Array

关键点:符合条件的empty spot 的 左右邻居也要为0

 class Solution
{
public boolean canPlaceFlowers(int[] flowerbed, int n)
{
int count = 0; // count the valid empty spot for(int i=0; i<flowerbed.length; i++)
{ if(flowerbed[i] == 0) // if find empty spot, check its adjacent spots
{
if(i-1 >= 0 && flowerbed[i-1] == 1) // check left adjacent spot
continue;
if(i+1 < flowerbed.length && flowerbed[i+1] == 1) // check right adjacent spot
continue; // if come here, meaning this 0 is valid spot to place flower
count++;
i++; // skip next 0
}
else if(flowerbed[i] == 1) // if find a flower spot, its next spot is not valid, skip it
{
i++;
} if(count >= n) // if there are enough spots to place flower, no need to continue
return true; } return false;
}
}

参考资料:N/A

LeetCode 题目列表 - LeetCode Questions List

LeetCode 605. Can Place Flowers (可以种花)的更多相关文章

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

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

  2. 605. Can Place Flowers【easy】

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

  3. 【Leetcode_easy】605. Can Place Flowers

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

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

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

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

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

  6. Java实现 LeetCode 605 种花问题(边界问题)

    605. 种花问题 假设你有一个很长的花坛,一部分地块种植了花,另一部分却没有.可是,花卉不能种植在相邻的地块上,它们会争夺水源,两者都会死去. 给定一个花坛(表示为一个数组包含0和1,其中0表示没种 ...

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

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

  8. 【Leetcode】605. Can Place Flowers

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

  9. Leetcode 605.种花问题

    种花问题 假设你有一个很长的花坛,一部分地块种植了花,另一部分却没有.可是,花卉不能种植在相邻的地块上,它们会争夺水源,两者都会死去. 给定一个花坛(表示为一个数组包含0和1,其中0表示没种植花,1表 ...

随机推荐

  1. sql+PHP基础+面向对象基础简单总结

    一.MYSQL         1.配置MySql                 第一步安装服务器(apache).                 第二部安装MySql界面程序         2 ...

  2. Spring-Hibernate-web的延迟加载方案

    1,现象与问题 /** * 由于Hibernate存在延迟加载问题,当Dao事务提交之后,session就关闭: * 此时如果到显示层就没有办法获取对象,使用openSessionInViewer是解 ...

  3. Django实现内容缓存

    1.缓存的简介 在动态网站中,用户所有的请求,服务器都会去数据库中进行相应的增,删,查,改,渲染模板,执行业务逻辑,最后生成用户看到的页面. 当一个网站的用户访问量很大的时候,每一次的的后台操作,都会 ...

  4. Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/SpringStruts]]

    今天启动Tomcat时候遇到了这个问题 Failed to start component [StandardEngine[Catalina].StandardHost[localhost].Stan ...

  5. 配置exVim开发环境

    exVim主页 http://exvim.github.io/ 使用该配置原因: 简单,组织各种优秀插件,安装包很小,各种操作很流畅 实用,对于项目来说,只需要多出一个xx.exvim文件,所有符号等 ...

  6. BZOJ1854_游戏_KEY

    1854: [Scoi2010]游戏 Time Limit: 5 Sec Memory Limit: 162 MB Description lxhgww最近迷上了一款游戏,在游戏里,他拥有很多的装备, ...

  7. websocket的理解及实例应用

    websocket协议是HTML5提出的一个新的规范,主要用于实现服务器及时推送信息给客户端的功能. websocket实现是基于HTTP协议的部分握手功能,但是websocket仅仅握手一次即可进行 ...

  8. java 反射 类装载器

    前言: java语言允许通过程序化的方式间接对Class进行操作,Class文件由类装载器装载后,在jvm中将形成一份描述Class结构的元信息对象,通过该元信息对象可以获知Class的结构信息,如构 ...

  9. Maven在Windows中的配置以及IDE中的项目创建

    Maven在Windows下的配置 1.Maven下载地址:http://maven.apache.org/download.cgi,下载红框里的版本即可. 2.解压到D盘: 3.修改配置文件sett ...

  10. Linux入门之常用命令(7)压缩

    compress filename 压缩   -d解压缩  *.Z bzip -d解压缩 -z压缩 *.bz2 bzcat filename .bz2 读取压缩文件内容 gzip -d解压缩  -#压 ...