【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 ...
随机推荐
- Win7 自带FTP将文件复制到FTP服务器时发生错误。
错误截图: 错误信息: 将文件复制到FTP服务器时发生错误.请检查是否有权限将文件放到该服务器上. 详细信息: 200 Type set to I. 200 PORT commad successfu ...
- win7 64位 安装java jdk1.8 ,修改配置环境变量
下载jdk1.8,下载地址:http://www.wmzhe.com/soft-30118.html 安装时有两个程序,都安装在同一个目录下. win7 64位 安装java jdk1.8 ,修改配置 ...
- codis学习
一.codis-proxy 结构 1.Topology 2.Slots 3.ServerGroup 4.Server 二.codis-proxy 启动过程 1.初始化ProxyInfo Id ...
- Uva 12298 超级扑克2
题目链接:https://vjudge.net/problem/UVA-12298 题意: 1.超级扑克,每种花色有无数张牌,但是,这些牌都是合数:比如黑桃:4,6,8,9,10,,,, 2.现在拿走 ...
- 2014-2015 ACM-ICPC East Central North America Regional Contest (ECNA 2014) A、Continued Fractions 【模拟连分数】
任意门:http://codeforces.com/gym/100641/attachments Con + tin/(ued + Frac/tions) Time Limit: 3000/1000 ...
- 对枚举数据类型使用switch
- H5与安卓、IOS的交互,判断微信、移动设备、安卓、ios
一.通过用户代理可以判断网页当前所在的环境 var browser={ versions:function(){ var u = navigator.userAgent, app = navigato ...
- Ajax去除缓存
1.在Ajax发送请求钱加上anyAjaxObj.setRequestHeader("If-Modeified-Since","0").2.在Ajax发送请求钱 ...
- js正则判断日期
//****************************************************************************// Function ID : Commo ...
- 【洛谷P1107】 [BJWC2008]雷涛的小猫
雷涛的小猫 题目链接 n^2DP比较好想, f[i][j]表示第i棵树高度为j的最大收益 直接从上到下转移即可,每次记录下max f[1~n][j] 用于下面的转移 f[i][j]=max(f[i][ ...