【Leetcode】605. Can Place Flowers
Description
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.
Discuss
只需要计算出一共可以放多少个就可以了。
Code
class Solution {
public boolean canPlaceFlowers(int[] flowerbed, int n) {
int len = flowerbed.length;
if (len < n) { return false; }
int count = 0;
if (len == 1 && flowerbed[0] == 0) {
count++;
return true;
}
for (int i = 0; i < len; i++) {
if (i == 0) {
if (flowerbed[0] == 0 && flowerbed[1] == 0) {
count++;
flowerbed[0] = 1;
}
continue;
}
if (i == len-1) {
if (flowerbed[i - 1] == 0 && flowerbed[i] == 0) {
count++;
}
break;
}
if (flowerbed[i - 1] == 0 && flowerbed[i] == 0 && flowerbed[i + 1] == 0) {
count++;
flowerbed[i] = 1;
}
}
return count >= n;
}
}
【Leetcode】605. Can Place Flowers的更多相关文章
- 【LeetCode】605. Can Place Flowers 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 贪婪算法 日期 题目地址:https://leetcode.c ...
- 【Leetcode_easy】605. Can Place Flowers
problem 605. Can Place Flowers 题意: solution1: 先通过简单的例子(比如000)发现,通过计算连续0的个数,然后直接算出能放花的个数,就必须要对边界进行处理, ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
随机推荐
- [libxml2]_[XML处理]_[使用libxml2的xpath特性修改xml文件内容]
场景: 1.在软件需要保存一些配置项时,使用数据库的话比较复杂,查看内容也不容易.纯文本文件对utf8字符支持也不好. 2.这时候使用xml是最佳选择,使用跨平台库libxml2. 3.基于xpath ...
- [转]Ubuntu 小企鹅输入法fcitx 支持 五笔拼音
之前在Ubuntu下使用ibus五笔输入法,用了一段时间发现五笔输入法不能输入词组,并且五笔不支持拼音的功能,从网上找到可以使用fcitx替换掉ibus,因此自已尝试了一把,安装步骤如下: 1. 安装 ...
- note02-计算机网络
2.物理层 通信系统模型(源->发送器->XXX->接收器->终),源发出数字信号,调制器编码为模拟信号,进行传输,解调器解码模拟信号为数字信号给终 信噪比,香农公式,C=B ...
- ACM-ICPC(9/26)
DP专题 多阶段决策:递推——逆推方式(难度较大),记忆化搜索方式,考虑当前决策层(cur) 01背包:变形众多,两种方式,一是考虑阶段的方式, ,另一种是刷表法 题目推荐: bzoj 4247 De ...
- HDU 5805
题意: 退役狗 NanoApe 滚回去学文化课啦! 在数学课上,NanoApe 心痒痒又玩起了数列.他在纸上随便写了一个长度为 nn 的数列,他又根据心情随便删了一个数,这样他得到了一个新的数列,然后 ...
- 【[HEOI2016/TJOI2016]游戏】
据说是网络流棋盘模型了 我们把每一个连续子段都看成一个点,我们先把所有的行上的连续子段找出来给他们编上号,所有列上的连续子段找出来也编上号 现在每个格子都有两个编号了,\(a[i][j]\)表示行所对 ...
- css relative
一.relative和absolute相煎关系 relative限制absolute 1.限制left/top/right/bottom定位 如果父元素有relative,只能根据父元素进行定位 2. ...
- Android学习笔记_40_系统结构 目录结构
1.系统结构: 一.应用程序层 Android平台不仅仅是操作系统,也包含了许多应用程序,诸如SMS短信客户端程序.电话拨号程序.图片浏览器.Web浏览器等应用程序.这些应用程序都是用Java语言编写 ...
- 轻量ORM-SqlRepoEx (二)初始化SqlRepoEx
一.SqlRepoEx引用 暂时没放至nuget上,可以直接到https://github.com/AzThinker/SqlRepoEx下载源码,编译引用. (一).静态引用 1.需引用以下dll在 ...
- Windows 2008 server + IIS 7 设置身份模拟(ASP.NET impersonation)
IIS7 与 IIS 6 相比有了很大的改动,原来在 IIS 6 下可以的设置到了 IIS 7 下有的会发生变化.身份模拟的配置上,IIS7 和 IIS6有很大不同,网上IIS6的身份模拟的文章比较多 ...