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 ...
随机推荐
- level-8
CSS选择器常见的有几种? !important选择器——它是所有选择器里面权重最高的. *选择器——它是所有选择器里面权重最低的.可以为页面所有的标签添加统一样式,多用于页面整体样式调整,但它会增加 ...
- DNS 主从同步配置
DNS 主从同步配置 主从同步:主每次修改配置文件需要修改一下序列号,主从同步主要 看序列号. 从DNS:从是可以单独修改,主从不会报错.但从修改后,主端同步给从后 从端修改数据会丢失 主从原理:从会 ...
- canvas 视频音乐播放器
canvas 视频音乐播放器 var play_nor_img_path = 'images/play_btn_n.png'; //播放按钮 正常时 60x60 px var play_sec_img ...
- volatile笔记
总结自:https://www.cnblogs.com/dolphin0520/p/3920373.html 了解volatile之前得明白什么是原子性.可见性.有序性及指令重排序,详见:https: ...
- 20145230《Java程序设计》第5周学习总结
20145230 <Java程序设计>第5周学习总结 教材学习内容 本周主要学习的内容是关于异常处理的,感觉这部分内容对我们这种初学者 来说非常重要.举个例子,倘若你在编写一个Java程序 ...
- INSPIRED启示录 读书笔记 - 第12章 产品探索
软件项目可以划分为两个阶段 探索产品阶段:弄清楚要开发什么产品(定义正确的产品) 在探索产品的阶段,产品经理负责分析各种创意,广泛收集用户需求,了解如何运用新技术,拿出产品原型并加以测试 从全局视角思 ...
- 计数排序(COUNTING-SORTING)
计数排序的思想: 计数排序是对每一个输入元素x;确定小于x的元素个数. 计数排序算法: 第一个for循环为统计arra 中的每一个数值的个数,并且放在相应arrc 数组中的arra[i]位,第二个fo ...
- 加和求不同的组合方式数目(dp)
描述 有n个正整数,找出其中和为t(t也是正整数)的可能的组合方式.如: n=5,5个数分别为1,2,3,4,5,t=5: 那么可能的组合有5=1+4和5=2+3和5=5三种组合方式. 输入 输入的第 ...
- MapReduce-读取文件写入HBase
MapReduce直接写入HBase 代码如下 package com.hbase.mapreduce; import java.io.IOException; import org.apache.c ...
- PAT1058. A+B in Hogwarts (20)
#include <iostream> using namespace std; int ag,as,ak; int bg,bs,bk; int cg,cs,ck; int main(){ ...