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 ...
随机推荐
- HAProxy详解
HAProxy概述与配置 一.HAProxy概述 HAProxy是由 WillyTarreau开发的一款具备高可用性.负载均及基于 TCP和 HTTP的应用代理开源软件,基于HAProxy的负载均衡架 ...
- 以太网100Mhz频率为什么可以达到带宽1000Mbps
转: https://wenku.baidu.com/view/353ea8ecb0717fd5370cdc0b.html
- 【Flask】ORM关系以及一对多
### ORM关系以及一对多:mysql级别的外键,还不够ORM,必须拿到一个表的外键,然后通过这个外键再去另外一张表中查找,这样太麻烦了.SQLAlchemy提供了一个`relationship`, ...
- verilog FAQ(zz)
1. What is the race condition in verilog? Ans :The situation when two expressions are allowed to exe ...
- 2020年将热门的8大IT职业领域
近日,外媒梳理了未来5年内,也是就是2020年仍将受到热捧的八大科技领域,为IT从业者如何做好长远规划.有针对性地培养自身技能.又不偏离热门岗位提供了参考.(图片来自网易) 2020年将热门的8大IT ...
- 关于C++ 中的this 的理解
关键字this 通常被用在一个class内部,指正在被执行的该class的对象(object)在内存中的地址.它是一个指针,其值永远是自身object的地址.
- 输入框去除默认的文字,jquery方法
需求:所有的输入框获取焦点时,去掉默认的提示文字,失去焦点时如果输入框为空,恢复默认的提示文字. 解决方案:jquery方法,以下有三种,按照利弊,我建议最后一种. 先看html代码: <inp ...
- 算法总结之 数组的partition调整
给定一个有序数组arr, 调整arr使得这个数组的左半部分没有重复元素且升序,而且不用保证右边是否有序 分区就ok了 u区是 无重复且升序的 u是这个区域的最后位置,初始u=0 i做从左到右的遍历, ...
- 透透彻彻IoC
本文转载自:http://stamen.iteye.com/blog/1489223/ 引述:IoC(控制反转:Inverse of Control)是Spring容器的内核,AOP.声明式事务等功能 ...
- windows下pip安装包权限的问题
md哔了狗了,把scipy弄崩了,还顺带把numpy弄崩了... 然后安装包一直权限不允许: 于是按照下面这篇博客以管理员运行cmd,结果还是没卵用 http://www.cnblogs.com/li ...