LeetCode Can Place Flowers
原题链接在这里: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 ...
随机推荐
- docker 命令添加容器数据卷
实现宿主机和容器的数据共享 只要建立连接,即使容器exit,主机的修改仍能提现到容器
- 我的npm笔记
本文记录一些npm的使用技巧,主要包括自己常用的命令,做个备忘. NPM 是什么? NPM是NodeJS的包管理工具,现在已经进一步发展,致力于为很多其他平台提供包管理工具,其核心思想就是让包的安装更 ...
- PAT 天梯赛 L1-028. 判断素数 【水】
题目链接 https://www.patest.cn/contests/gplt/L1-028 AC代码 #include <iostream> #include <cstdio&g ...
- first application
<!DOCTYPE html> <html> <head> <title>Create a Map</title> <meta htt ...
- OPENGL ES2.0如何不使用glActiveTexture而显示多个图片
https://www.oschina.net/question/253717_72107 用opengl es 2.0显示多个图片的话,我只会一种方式,先将图片生成纹理,然后用下面的方式渲染 // ...
- CSS实现三角形图标的原理!!!!今天总算弄懂了。
网页中经常有一种三角形的图标,鼠标点一下会弹出一个下拉菜单之类的(之前淘宝也有,不过现在改版好像没有了) 之前以为是个png图标背景,后来在bootstrap中看到有一个图标样式叫做caret的用来实 ...
- no xxx find in java.library.path
JAVA系统运行时候load native lib时候会遇到下面错误,如 java.lang.UnsatisfiedLinkError: no JSTAF in java.library.path这可 ...
- 模型融合之blending和stacking
1. blending 需要得到各个模型结果集的权重,然后再线性组合. """Kaggle competition: Predicting a Biological Re ...
- recovery 差分升级包制作超时【转】
本文转载自:https://blog.csdn.net/csdn66_2016/article/details/73800349 我们在对android系统升级的时候,可以减少升级包的大小,只升级差异 ...
- java resources 红叉 An error occurred while filtering resources
用eclipse创建了一个Spring mvc的Maven项目,在项目上有个叉叉,通过Window -> Show View -> Markers中看到错误原因 An error occu ...