原题链接在这里:https://leetcode.com/problems/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
Note:
- The input array won't violate no-adjacent-flowers rule.
- The input array size is in the range of [1, 20000].
- n is a non-negative integer which won't exceed the input array size.
题解:
当前值为0时看前一个和后一个是否同时为0, 若是就可以栽花. 注意首尾特殊情况.
Time Complexity: O(flowerbed.length).
Space: O(1).
AC Java:
class Solution {
public boolean canPlaceFlowers(int[] flowerbed, int n) {
if(flowerbed == null){
throw new IllegalArgumentException("Input array is null.");
} int count = 0;
for(int i = 0; i<flowerbed.length && count<n; i++){
if(flowerbed[i] == 0){
int pre = (i == 0) ? 0 : flowerbed[i-1];
int next = (i == flowerbed.length-1) ? 0 : flowerbed[i+1];
if(pre == 0 && next == 0){
count++;
flowerbed[i] = 1;
}
}
}
return count == n;
}
}
LeetCode Can Place Flowers的更多相关文章
-
[LeetCode] 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 (可以种花)
Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, ...
-
LeetCode 605. 种花问题(Can Place Flowers) 6
605. 种花问题 605. Can Place Flowers 题目描述 假设你有一个很长的花坛,一部分地块种植了花,另一部分却没有.可是,花卉不能种植在相邻的地块上,它们会争夺水源,两者都会死去. ...
-
【LeetCode】605. Can Place Flowers 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 贪婪算法 日期 题目地址:https://leetcode.c ...
-
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算法题-Can Place Flowers(Java实现)
这是悦乐书的第272次更新,第287篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第140题(顺位题号是605).假设你有一个花坛,其中一些地块是种植的,有些则不是. 然 ...
-
【Leetcode】605. Can Place Flowers
Description Suppose you have a long flowerbed in which some of the plots are planted and some are no ...
-
C#LeetCode刷题之#605-种花问题( Can Place Flowers)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3724 访问. 假设你有一个很长的花坛,一部分地块种植了花,另一部 ...
-
LeetCode第四天
leetcode 第四天 2018年1月4日 15.(628)Maximum Product of Three Numbers JAVA class Solution { public int max ...
随机推荐
-
Unity3D代码旋转
subGameObject.transform.Rotate (,,,Space.Self); 一行代码
-
忽然发现,if语句没有相应的continue功能
就是剩下部分语句不用执行了,但是又不退出当前函数,只退出当前if块.虽说else可以解决问题,但是这样还是会重复写代码,假如continue语句后面的内容是相同的话.当然可以通过再次加一个if语句解决 ...
-
hdu3278Puzzle
其实最终的结果无非是中间8个方块是相同的颜色,外面16个方块是另外两种颜色.那么其实可以把外面两种颜色看作是0,中间的颜色看做是1. 那么题目就变成了把那种颜色看做1,而其它两种颜色看做0,可以用最少 ...
-
检索字符创 php
strstr()可以返回匹配的值 echo strstr("localhost", "os");返回ost echo substr_count("gg ...
-
Kafka 在行动:7步实现从RDBMS到Hadoop的实时流传输
原文:https://coyee.com/article/11095-kafka-in-action-7-steps-to-real-time-streaming-from-rdbms-to-hado ...
-
UOJ#291. 【ZJOI2017】树状数组 树套树
原文链接https://www.cnblogs.com/zhouzhendong/p/UOJ291.html 题解 结论:这个写错的树状数组支持的是后缀加和后缀求和.这里的后缀求和在 x = 0 的时 ...
-
GIL学习
GIL锁 一.GIL的简单概述 二.GIL对于多线程的影响 三.解决GIL对于多线程影响的方案 回到顶部 一.GIL的简单概述 1.概念 GIL ( Global Interperter Lock ) ...
-
.NET代码混淆——开源.net 混淆器ConfuserEx介绍
转载:https://blog.csdn.net/xiaoyong_net/article/details/78988264
-
【codevs2205】等差数列
题目大意:给定一个长度为 N 的序列,求这个序列中等差数列的个数. 题解:根据题意应该是一道序列计数 dp.设 \(dp[i][j]\) 表示以第 i 项结尾,公差为 j 的等差数列的个数,则状态转移 ...
-
python 杂谈
python 当前文件导入自定义模块的时候,会默认执行一遍 python使用的变量必须是已经定义或者声明过的.