zoukankan      html  css  js  c++  java
  • LeetCode 605. Can Place Flowers (可以种花)

    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

     1 class Solution 
     2 {
     3     public boolean canPlaceFlowers(int[] flowerbed, int n) 
     4     {
     5         int count = 0; // count the valid empty spot
     6         
     7         for(int i=0; i<flowerbed.length; i++)
     8         {
     9 
    10             if(flowerbed[i] == 0) // if find empty spot, check its adjacent spots
    11             {
    12                 if(i-1 >= 0 && flowerbed[i-1] == 1) // check left adjacent spot
    13                     continue;
    14                 if(i+1 < flowerbed.length && flowerbed[i+1] == 1) // check right adjacent spot
    15                     continue;
    16                 
    17                 // if come here, meaning this 0 is valid spot to place flower
    18                 count++;
    19                 i++; // skip next 0
    20             }
    21             else if(flowerbed[i] == 1) // if find a flower spot, its next spot is not valid, skip it
    22             {
    23                 i++;
    24             }
    25             
    26             
    27             if(count >= n) // if there are enough spots to place flower, no need to continue
    28                 return true;
    29             
    30         }
    31         
    32         return false;
    33     }
    34 }

    参考资料:N/A

    LeetCode 题目列表 - LeetCode Questions List

  • 相关阅读:
    记录使用cx_Freeze打包Python成exe可执行程序
    jquery plug-in DataTable API中文文档参考
    java.lang.UnsatisfiedLinkError: dlopen failed: /data/app/xxx/lib/arm/liblame.so: has text relocations
    CocoaPods的安装及使用
    Android 贝塞尔曲线的浅析
    GUI学习中错误Exception in thread "main" java.lang.NullPointerException
    线程
    12月13日
    今天开始学习java编程
    UVA10140 Prime Distance
  • 原文地址:https://www.cnblogs.com/jimmycheng/p/7675373.html
Copyright © 2011-2022 走看看