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
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
- for循环一般还是从0到n, 如果怀疑,先做个标记,后续再改
[思维问题]:
for循环之内应该满足一个第一步的初始条件,再进行后续操作
[一句话思路]:
正常操作,直面灵魂拷问
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 最多能种的花要比实际给的花的额度更大,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零一间隔种花的更多相关文章
- 605. Can Place Flowers【easy】
605. Can Place Flowers[easy] Suppose you have a long flowerbed in which some of the plots are plante ...
- 【Leetcode_easy】605. Can Place Flowers
problem 605. Can Place Flowers 题意: solution1: 先通过简单的例子(比如000)发现,通过计算连续0的个数,然后直接算出能放花的个数,就必须要对边界进行处理, ...
- 605. Can Place Flowers种花问题【leetcode】
Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, ...
- LeetCode 605. Can Place Flowers (可以种花)
Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, ...
- 【LeetCode】605. Can Place Flowers 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 贪婪算法 日期 题目地址:https://leetcode.c ...
- 605. Can Place Flowers
Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, ...
- 【Leetcode】605. Can Place Flowers
Description Suppose you have a long flowerbed in which some of the plots are planted and some are no ...
- LeetCode 605. 种花问题(Can Place Flowers) 6
605. 种花问题 605. Can Place Flowers 题目描述 假设你有一个很长的花坛,一部分地块种植了花,另一部分却没有.可是,花卉不能种植在相邻的地块上,它们会争夺水源,两者都会死去. ...
- LeetCode(605,581,566)
LeetCode(605,581,566) 摘要:605盲改通过:581开始思路错误,后利用IDE修改(多重循环跳出方法):566用C语言时需要动态内存分配,并且入口参数未能完全理解,转用C++. 6 ...
随机推荐
- HTML服务器控件与Web服务器控件
asp.net之所以现在开发方便和快捷,关键是它有一组强大的控件库,包括web服务器控件,web用户控件,web自定义控件,html服务器控件和html控件等.这里主要整理一下html控件.html服 ...
- Hexo博客网站再配置
这两天整理网站方面的事,本地IIS部署,个人网站,发现我的hexo做的个人网站实在很单调,于是找来资料做进一步的配置. 一.网站图标 看一下hexo\themes\modernist\layout\_ ...
- Spring IOC容器的初始化—(一)Resource定位
前言 上一篇博文“ Spring IOC是怎样启动的 ”中提到了refresh()方法,这个就是容器初始化的入口.容器初始化共有三个阶段: 第一阶段:Resource定位 第二阶段:BeanDefin ...
- webpack新版本4.12应用九(配置文件之模块(module))
这些选项决定了如何处理项目中的不同类型的模块. module.noParse RegExp | [RegExp] RegExp | [RegExp] | function(从 webpack 3.0. ...
- openfaas 了解
1. 官方介绍 OpenFaaS (Functions as a Service) is a framework for building serverless functions with Dock ...
- Anaconda 使用conda常用命令
1.首先在所在系统中安装Anaconda.可以打开命令行输入conda -V检验是否安装以及当前conda的版本. 2.conda常用的命令. 1)conda list 查看安装了哪些包. 2)con ...
- w3cschool在线教程
做网页开发的,没有不知道w3cschool的,如果你还不知道,那么就应该早点看下面推荐的文章,菜鸟可以帮你提升你的技能,老鸟可以温故而知新. 第一个是:http://www.w3school.com. ...
- SVN报错:sqlite[S5]:database is locked
昨天下午修改几个冲突的jar包后提交svn后报错,接下来svn操作就失灵了,无论是clean up还是revert还是release lock都无济于事.解决办法: 首先下载sqlite3,我的是64 ...
- ubuntu14.04安装pyspider
sudo apt-get install libcurl4-openssl-dev libxml2-dev libxslt1-dev sudo atp-get install phantomjs 激活 ...
- CAN总线标准帧
CAN总线是一种串行数据通信协议,其通信接口中集成了CAN协议的物理层和数据链路层功能,可完成对通信数据的成帧处理,包括位填充.数据块编码.循环冗余检验.优先级判别等项工作. CAN总线结构 CAN总 ...