[抄题]:

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

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

  1. for循环一般还是从0到n, 如果怀疑,先做个标记,后续再改

[思维问题]:

for循环之内应该满足一个第一步的初始条件,再进行后续操作

[一句话思路]:

正常操作,直面灵魂拷问

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 最多能种的花要比实际给的花的额度更大,count >= n

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

[复杂度]:Time complexity: O(n) Space complexity: O(1)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[关键模板化代码]:

正常操作,直面灵魂拷问

return count >= n;

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

class Solution {
public boolean canPlaceFlowers(int[] flowerbed, int n) {
//cc
if (flowerbed == null || flowerbed.length == 0) {
return false;
} //ini
int count = 0; //for loop as normal
for (int i = 0; i < flowerbed.length && count <= n; i++) {
if (flowerbed[i] == 0) {
int prev = (i == 0) ? 0 : flowerbed[i - 1];
int next = (i == flowerbed.length - 1) ? 0 : flowerbed[i + 1]; if (prev == 0 && next == 0) {
count++;
flowerbed[i] = 1;
}
}
} return count >= n;
}
}

605. Can Place Flowers零一间隔种花的更多相关文章

  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. 605. Can Place Flowers种花问题【leetcode】

    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 (可以种花)

    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. 605. Can Place Flowers

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

  7. 【Leetcode】605. Can Place Flowers

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

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

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

  9. LeetCode(605,581,566)

    LeetCode(605,581,566) 摘要:605盲改通过:581开始思路错误,后利用IDE修改(多重循环跳出方法):566用C语言时需要动态内存分配,并且入口参数未能完全理解,转用C++. 6 ...

随机推荐

  1. sublime 非常好用的注释工具

    Sublime在进行前端开发时非常棒,当然也少不了众多的插件支持,DocBlocker是在Sublime平台上开发一款自动补全代码插件,支持JavaScript (including ES6), PH ...

  2. python Beautiful Soup的使用

    上一节我们介绍了正则表达式,它的内容其实还是蛮多的,如果一个正则匹配稍有差池,那可能程序就处在永久的循环之中,而且有的小伙伴们也对写正则表 达式的写法用得不熟练,没关系,我们还有一个更强大的工具,叫B ...

  3. python学习-实现用户密码登录,输错三次锁定

    作业需求: 输入用户名密码 认证成功后显示欢迎信息 输错三次后锁定 实现思路: 判断用户是否在黑名单,若在黑名单,则将用户锁定 判断用户是否存在,若不存在,提示用户不存在 若用户存在,判断登录密码是否 ...

  4. 20165222 预备作业3 Linux安装及学习

    一,安装Linux操作系统 一开始下载ubuntu的时候比较麻烦,然后用同学发给我的,才得以安装.根据教程,也没出现什么问题,只是下载的比较麻烦.  二,关于Linux的学习 1,Linux系统简介和 ...

  5. IQ/OQ/DQ/PQ

    Installation Qualification (IQ) 安装验证,安装时在用户处进行.它提供文件证明用户安装环境所有的规格及参数完全符合制造商的描述和安全 要求. Operation Qual ...

  6. mysql安装问题汇总

    操作系统:win7 mysql版本号:mysql 5.5(64位) 1.又一次安装mysql卡在最后一步过不去 问题描写叙述:第一次安装完mysql.使用jdbc写入的中文都是乱码.但已存在于数据库中 ...

  7. 如何给 FastAdmin 单独设置域名

    如何给 FastAdmin 单独设置域名 (声明:不建议给后台固定的域名,主要是安全问题) FastAdmin 是基于 ThinkPHP5 框架编写的,ThinkPHP 5 支持域名路由,可对模块单独 ...

  8. jinja2的一些用法

    1.split用法以及乘法运算 {% set user_list=l.users.split(',') %} <tr> <td>{{ l.name }}</td> ...

  9. AppCan使用注意问题

    1.文件上传的时候尽量使用uexUploadMsg,然后注意文件名,文件名一定要正确才能传上去.

  10. PHP5.3.8连接Sql Server SQLSRV30

    PHP5.3连接SQL Server就不能用php_mssql.dll了. 网上下载了好多都不行,因为它的版本是5.2的,不能再PHP5.3中使用. 后来听说微软专门为PHP出了自己的dll. 叫做M ...