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 ...
随机推荐
- [转载]Lwip之IP/MAC地址冲突检测
from: http://blog.csdn.net/tianjueyiyi/article/details/51097447 LWIP是个轻量级的TCP/IP协议栈,之所以说轻量级,是因为作者将主体 ...
- HihoCoder1619 “共同富裕”(逆向思维)
“共同富裕” 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个数组A1, A2, ... AN,每次操作可以从中选定一个元素Ai,把除了Ai之外的所有元素都加1. ...
- Python之numpy库
NumPy库知识结构 更多详细内容参考:http://www.cnblogs.com/zhanglin-0/p/8504635.html
- Java得到当前系统时间,精确到毫秒的几种方法
import java.text.SimpleDateFormat; import java.util.Date; import java.util.Calendar; public class Ma ...
- Stars
Astronomers often examine star maps where stars are represented by points on a plane and each star h ...
- RecyclerView 初体验
网上看了很多 RecyclerView 的教程,也结合学长的代码,终于实现了一个不错看的过去的List 可以通过左滑删除Item 长按Item或者点击按钮,可以对Item进行拖拽 更具体的内容会写在代 ...
- LG3195 [HNOI2008]玩具装箱TOY
题意 P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再放到一种特殊的一维容器中.P教授有编号为\(1\cdots N\) ...
- 十九、python沉淀之路--装饰器
一.实现装饰器的预备知识 装饰器 = 高阶函数 + 函数嵌套 + 闭包 1.高价函数定义: 1.函数接收的参数是一个函数名 2.函数的返回值是一个函数名 3.满足上述条件任意一个,都可称之 ...
- python操作rabbitmq、redis
1.启动rabbimq.mysql 在“”运行“”里输入services.msc,找到rabbimq.mysql启动即可 2.启动redis 管理员进入cmd,进入redis所在目录,执行redis- ...
- QtAV的编译方法
1--编译准备 QtAV的安装编译总指导说明:https://github.com/wang-bin/QtAV/wiki/Build-QtAV QtAV的源代码:https://github.com/ ...