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. Activiti-01

    1, Activiti官网:http://www.activiti.org/  主页可以看到jar包的下载. 2, 进入http://www.activiti.org/userguide/index. ...

  2. java-annotation的简单介绍

    package com.yangwei.shop.entity; /** * annotation作用 一是进行标识,二是进行约束 * *///必须让它在运行时能够执行@Retention(Reten ...

  3. 我和Python

    记不得是年,我在网易云课堂上乱逛,看到了哈佛大学的<计算机编程导论>,这门课讲的正好是Python,讲的啥内容已经记不得多少了,因为是全英文教学,我只能慢慢的看字幕,一集得看个好几遍. 我 ...

  4. 【Spring源码深度解析系列 】Spring整体架构

    一.Spring的整体架构和模块 二.模块分类: 1.Core Container Core Container包含有Core .Beans.Context.和Expression  Language ...

  5. angular 如何获取使用filter过滤后的ng-repeat的数据长度

    在做项目的过程中,被产品要求在内容为空的过程中显示提示信息,然哦户内容使用ng-repeat循环输出的,并且使用了filter过滤.后在谷歌上找到解决方案,如下: ​之前代码如下显示: <ul& ...

  6. 前端系列——jquery前端国际化解决方案“填坑日记”

    前言:最近,新的平台还没有开发完成,原来的老项目又提出了新的需求:系统国际化.如果是前后端完全分离的开发模式,要做国际化,真的太简单了,有现成的解决方案,基于Node构建的时下热门的任何一种技术选型都 ...

  7. #define WIN32_LEAN_AND_MEAN

    不加载MFC所需的模块.用英语解释:Say no to MFC如果你的工程不使用MFC,就加上这句,这样一来在编译链接时,包括最后生成的一些供调试用的模块时,速度更快,容量更小.不过对于较大工程,MF ...

  8. stdafx.h 的作用

    stdafx.h VC工程里面经常见到stdafx.h这个头文件,以前也没有特别注意,但是这个文件用不好经常会出错. stdafx的英文全称为:Standard Application Framewo ...

  9. Spring-Boot:Spring Cloud构建微服务架构

    概述: 从上一篇博客<Spring-boot:5分钟整合Dubbo构建分布式服务> 过度到Spring Cloud,我们将开始学习如何使用Spring Cloud 来搭建微服务.继续采用上 ...

  10. Java比较器

    导语 本节内容,比较器Comparable是核心内容. 主要内容 重新认识Arrays类 两种比较器的使用 具体内容 Arrays类 在之前一直使用的"java.util.Arrays.so ...