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. websphere部署 hibernate jpa & Error 500: javax/persistence/OneToOne.orphanRemoval()Z

    WebSphere 7 & Javax/Persistence/OneToMany.OrphanRemoval() Error 文章出处:http://www.mkyong.com/websp ...

  2. MySQL集群(四)之keepalived实现mysql双主高可用

    前面大家介绍了主从.主主复制以及他们的中间件mysql-proxy的使用,这一篇给大家介绍的是keepalived的搭建与使用! 一.keepalived简介 1.1.keepalived介绍 Kee ...

  3. uvalive 3708 Graveyard

    https://vjudge.net/problem/UVALive-3708 题意: 一个长度为10000的圆环上放着n个雕塑,每个雕塑之间的距离均相等,即这个圆环被n个点均分.现在需要加入m个雕塑 ...

  4. 一个非常好用的框架-AngularJS(一)

      前  言           AngularJS诞生于2009年,由Misko Hevery 等人创建,后为Google所收购.是一款优秀的前端JS框架,已经被用于Google的多款产品当中.An ...

  5. Python之面向对象与类

    本节内容 面向对象的概念 类的封装 类的继承 类的多态 静态方法.类方法 和 属性方法 类的特殊成员方法 子类属性查找顺序 一.面向对象的概念 1. "面向对象(OOP)"是什么? ...

  6. Problem 2062 Suneast & Yayamao 二进制(多重背包的理解基础)

                                          Problem 2062 Suneast & Yayamao Accept: 143    Submit: 313T ...

  7. centos crontab(定时任务) 使用

    一.介绍   crontab命令的功能是在一定的时间间隔调度一些命令的执行.当安装完成操作系统之后,默认便会启动此任务调度命令.crond命令每分锺会定期检查是否有要执行的工作,如果有要执行的工作便会 ...

  8. php使用PHPMailer邮件类发送邮件

    PHPMailer是一个用于发送电子邮件的PHP函数包.它提供的功能包括:*.在发送邮时指定多个收件人,抄送地址,暗送地址和回复地址*.支持多种邮件编码包括:8bit,base64,binary和qu ...

  9. DataGridView一些常用操作

    SqlConnection conn = new SqlConnection('Server=(local);DataBase=test;User=sa;Pwd=sa'); SqlDataAdapte ...

  10. gitlab服务器搭建教程

    gitlab服务器搭建教程 ----2016年终总结 三 参考https://bbs.gitlab.cc/topic/35/gitlab-ce-8-7-%E6%BA%90%E7%A0%81%E5%AE ...