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. JAVA基础——编程练习(一)

    java编程练习(一) 编程题目: 请根据所学知识,编写一个 JAVA 程序,实现输出考试成绩的前三名. 要求: 1. 考试成绩已保存在数组 scores 中,数组元素依次为 89 , -23 , 6 ...

  2. ovs+dpdk numa感知特性验证

    0.介绍 本测试是为了验证这篇文章中提到的DPDK的NUMA感知特性. 简单来说,在ovs+dpdk+qemu的环境中,一个虚拟机牵涉到的内存共有三部分: DPDK为vHost User设备分配的De ...

  3. Redis事务原理分析

    Redis事务原理分析 基本应用 在Redis的事务里面,采用的是乐观锁,主要是为了提高性能,减少客户端的等待.由几个命令构成:WATCH, UNWATCH, MULTI, EXEC, DISCARD ...

  4. Loadrunner--自动关联和手动关联

    2017-06-09 15:32:45个人也属于刚刚开始学习,有什么不对的地方敬请指导:qq:389791447 一开始的时候,准备去学习怎么去关联.一时也毛不着头脑,就在网上找了一些视频看,有的人说 ...

  5. Python 操作 MYSQL

    本文介绍了 Python 操作 MYSQL.执行 SQL 语句.获取结果集.遍历结果集.取得某个字 段.获取表字段名.将图片插入数据库.执行事务等各种代码实例和详细介绍,代码居多, 是一桌丰盛唯美的代 ...

  6. document.querySelectorAll() 与document.getElementTagName() 的区别

    这个区别我估计大神都不知道,问题源于博主,细节被一个妹子发现的 事情经过是这样 <ul> <li>item</li> <li></li> & ...

  7. poj 1948 Triangular Pastures 小结

    Description Like everyone, cows enjoy variety. Their current fancy is new shapes for pastures. The o ...

  8. Unity3D-Shader-实现X光效果

    [旧博客转移 - 2016年1月3日 16:40 ] 最近学习了一些Shader效果,打算把学到的知识总结一下,这篇讲一下这种轮廓发光的效果(如下图所示),也有一些地方管这个叫X光     1.原理 ...

  9. FineReport填报分页设置

    1. 问题描述 进行FineReport数据填报时,如果数据量过大,由于前端浏览器的性能限制,如果将数据全部展现出来,速度会非常的慢,影响用户体验,这时候大家就会想,填报是否能像分页预览一样进行分页呢 ...

  10. Selenium的简单安装和使用

    Selenium的安装 pip install selenium Selenium模块需要调用浏览器,需要配置selenium的浏览器驱动 Firefox(火狐) 下载对应版本的geckdriver. ...