[抄题]:

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. MyBatis对多关系:显示该用户的所有角色

    只要在一边的UserMapper.xml 配置好就可以了 <?xml version="1.0" encoding="UTF-8" ?> <! ...

  2. Hihocoder1081 最短路径 结构体练习

    最短路径 :虽然做过很多次最短路的题,spfa,bellman,floyd都用过不少.但是题目强调的“顺序”又让我加深了对最短路的理解. 当然,主要还是练习下STL: pair型: #include& ...

  3. CS与BS区别

    简介:CS即Client/Server(客户机/服务器)结构,C/S结构在技术上很成熟,它的主要特点是交互性强.具有安全的存取模式.网络通信量低.响应速度快.利于处理大量数据.但是该结构的程序是针对性 ...

  4. jfrog artifactory jenkins pipeline 集成

    1. 预备环境 artifactory ( 开源版本 ) maven jenkins jenkins artifactory plugin (在插件管理安装即可) 2. 配置artifactory  ...

  5. 【Xamarin】MonoTouch - iOS 使用 UIImagePickerController 打开图片库和相机选择图片修改头像

    Application tried to present modally an active controller <UIImagePickerController: 0x7b6ff400> ...

  6. apt安装工具

    apt-cache search package 搜索包 apt-cache show package 获取包的相关信息,如说明.大小.版本等 sudo apt-get install package ...

  7. Python中if __name__ == 'main' 的作用和原理

    参考网址:http://mp.weixin.qq.com/s/kxxhOQ7KB_VMwWeUENX7OQ t1.py: print('Loving Python') def main(): prin ...

  8. 【openCV学习笔记】在Mac上配置openCV步骤详解

    (1)安装Homebrew:(需要Ruby) 注:因为snow leopard 以后已经自带Ruby了,所有可以不用自己安装Ruby. 看一下Homebrew的官网: http://mxcl.gith ...

  9. shell中把大写字母转换成小写字母

    shell中把大写字母转换成小写字母 参考:http://www.jb51.net/article/40257.htm echo "AABBCC" | tr "[:upp ...

  10. 安装android studio时候弹出unable to access android sdk add-on list解决方法

    本文转载自:http://www.cnblogs.com/rancvl/p/6081791.html Android Studio First Run 检测 Android SDK 及更新,由于众所周 ...