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 nnew 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.

枚举法写出来 答案,计算可以种花总数是否比n大即可,有投机的成分

//如果这个东西是奇偶数,如果里面的客房偶数比b大即可
/*
左为0 当前为0 且右边为0或边界 这时改为1 count++
左为0 当前为1 继续
左为1 当前为0 继续
左为1 当前为1 继续
*/
public class Solution {
public boolean canPlaceFlowers(int[] flowerbed, int n) {
int count =0;
int len=flowerbed.length; if(len==1&&flowerbed[0]==0){
count++;
}
if(len==2&&(flowerbed[0]==0&&flowerbed[1]==0)){
count++;
}
for(int i=1;len>1&&i<len-1;i++){
if(i==1&&flowerbed[i]==0&&flowerbed[i-1]==0){
flowerbed[i-1] =1;
count++;
}
if((flowerbed[i]==0)&&(flowerbed[i]==flowerbed[i-1])&&(flowerbed[i]==flowerbed[i+1])){
flowerbed[i]=1;
count++;
}
if(i==len-2&&flowerbed[i]==0&&flowerbed[i+1]==0){
flowerbed[i+1] =1;
count++;
}
}
if(count>=n)
{return true;}
return false;
}
}

第二种办法,根据一次写的边界值进行归类

因为只需要判断当前元素如果是0的时候与左、右边界值(如果存在着)进行对比

//如果这个东西是奇偶数,如果里面的客房偶数比b大即可
/*
左为0 当前为0 且右边为0或边界 这时改为1 count++
左为0 当前为1 继续
左为1 当前为0 继续
左为1 当前为1 继续
*/
public class Solution {
public boolean canPlaceFlowers(int[] flowerbed, int n) {
int count =0;
int len=flowerbed.length;
//boolean flag =flase;
int right=-1;
int left =-1;
for(int i=0;i<len;i++){
//判断当前是否为0,如果为0,且右边有值则比较 否则直接改
int mid=flowerbed[i]; if(mid==0){
//右边界
if(i==0){
if(i+1<len){
right =flowerbed[i+1];
if(mid==right){
flowerbed[i]=1;
count++;
} }
else {
flowerbed[i]=1;
count++;
} }
//右边界
if(i==len-1){
if(i-1>=0){
left =flowerbed[i-1];
if(mid==left){
flowerbed[i]=1;
count++;
}
}
else {
flowerbed[i]=1;
count++;
}
}
//中间值
if(i-1>=0&&i+1<len){
left =flowerbed[i-1];
right =flowerbed[i+1];
if(left==mid&&mid==right){
flowerbed[i]=1;
count++;
}
} } }
if(count>=n)
{return true;}
return false;
}
}

官方答案1

public class Solution {
public boolean canPlaceFlowers(int[] flowerbed, int n) {
int i = 0, count = 0;
while (i < flowerbed.length) {
if (flowerbed[i] == 0 && (i == 0 || flowerbed[i - 1] == 0) && (i == flowerbed.length - 1 || flowerbed[i + 1] == 0)) {
flowerbed[i++] = 1;
count++;
}
if(count>=n)
return true;
i++;
}
return false;
}
}

T :o(n) Space:O(1)

精简的写法

public class Solution {
public boolean canPlaceFlowers(int[] flowerbed, int n) {
int count =0;
int len=flowerbed.length; for(int i=0;i<len;i++){
//判断当前是否为0,如果为0,且左右相邻均为0则可以种花,种花后修改花盆属性和种花数量
if(flowerbed[i]==0){
int right=(i==len-1)?0:flowerbed[i+1];
int left=(i==0)?0:flowerbed[i-1];
if(left==right&&right==0){
flowerbed[i]=1;
count++;
}
}
if(count>=n){
return true;
}
} return false;
}
}

605. Can Place Flowers种花问题【leetcode】的更多相关文章

  1. 605. Can Place Flowers【easy】

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

  2. 【Leetcode_easy】605. Can Place Flowers

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

  3. LeetCode 605. Can Place Flowers (可以种花)

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

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

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

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

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

  6. 【Leetcode】605. Can Place Flowers

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

  7. 605. Can Place Flowers

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

  8. Leetcode605.Can Place Flowers种花问题

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

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

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

随机推荐

  1. Chapter 3. Video Coding Concepts

    本章主要介绍一些有关视频编码的概念 时域模型(Temporal Model) 时域模型的作用是去除帧间冗余.如:将第二帧减去第一帧,得到的剩余信息,其能量会远小于第二帧本身. 基于块的运动估计和补偿 ...

  2. 利用MUI滑动进行利息计算(移动端APP显示)

    在开发移动端的应用时,会用到很多的手势操作,比如滑动.长按等,为了方便开放者快速集成这些手势,mui内置了常用的手势事件,其中滑动应用是比较常见的应用操作,本篇文章将讲述如何利用滑动改变对应值进行计算 ...

  3. Unity 游戏框架搭建 (四) 简易有限状态机

    为什么用有限状态机?   之前做过一款跑酷游戏,跑酷角色有很多状态:跑.跳.二段跳.死亡等等.一开始是使用if/switch来切换状态,但是每次角色添加一个状态(提前没规划好),所有状态处理相关的代码 ...

  4. 计算机程序的思维逻辑 (91) - Lambda表达式

    ​在之前的章节中,我们的讨论基本都是基于Java 7的,从本节开始,我们探讨Java 8的一些特性,主要内容包括: 传递行为代码 - Lambda表达式 函数式数据处理 - 流 组合式异步编程 - C ...

  5. XML读取两种方法

    //第一种SAX方法解析 package a20170722.xmlex; import java.io.File; import java.util.ArrayList; import java.u ...

  6. servlet+jsp导入Excel到mysql数据库

    package khservlet; import java.io.FileInputStream;import java.io.IOException;import java.io.InputStr ...

  7. 怎样把echarts图表做成响应式的

    如果想要把echarts图表给做成响应式的那么就应该用rem 单位,给图表的外围容器设置rem 单位,然后调用jquery 的resize方法,$(window).resize(function(){ ...

  8. vijos1062题解

    题目: 交谊舞是2个人跳的,而且一男一女 -____-||||. 由于交谊舞之前的节目安排,所有的表演者都站成了一排.这一排人的顺序满足2点: ①对于一对舞伴男生站在女生的左边. ②任何一对舞伴之间, ...

  9. Java异常体系简析

    最近在阅读<Java编程思想>的时候看到了书中对异常的描述,结合自己阅读源码经历,谈谈自己对异常的理解.首先记住下面两句话: 除非你能解决这个异常,否则不要捕获它,如果打算记录错误消息,那 ...

  10. 面向对象15.3String类-常见功能-获取-1

    API使用: 查API文档的时候,有很多方法,首先先看返回的类型 下面的方法函数有的是有覆写Object类的如1.1图,如果没有复写的话是写在1.2图片那里的,如果找到了相对于的方法,可以点击进去可以 ...