原题链接在这里: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:

  1. The input array won't violate no-adjacent-flowers rule.
  2. The input array size is in the range of [1, 20000].
  3. 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的更多相关文章

  1. [LeetCode] Can Place Flowers 可以放置花

    Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, ...

  2. LeetCode 605. Can Place Flowers (可以种花)

    Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, ...

  3. LeetCode 605. 种花问题(Can Place Flowers) 6

    605. 种花问题 605. Can Place Flowers 题目描述 假设你有一个很长的花坛,一部分地块种植了花,另一部分却没有.可是,花卉不能种植在相邻的地块上,它们会争夺水源,两者都会死去. ...

  4. 【LeetCode】605. Can Place Flowers 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 贪婪算法 日期 题目地址:https://leetcode.c ...

  5. 605. Can Place Flowers种花问题【leetcode】

    Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, ...

  6. LeetCode算法题-Can Place Flowers(Java实现)

    这是悦乐书的第272次更新,第287篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第140题(顺位题号是605).假设你有一个花坛,其中一些地块是种植的,有些则不是. 然 ...

  7. 【Leetcode】605. Can Place Flowers

    Description Suppose you have a long flowerbed in which some of the plots are planted and some are no ...

  8. C#LeetCode刷题之#605-种花问题( Can Place Flowers)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3724 访问. 假设你有一个很长的花坛,一部分地块种植了花,另一部 ...

  9. LeetCode第四天

    leetcode 第四天 2018年1月4日 15.(628)Maximum Product of Three Numbers JAVA class Solution { public int max ...

随机推荐

  1. Ag-grid控件使用pine:left后,配合iview下拉框,会出现闪烁

    Ag-grid控件使用pinned:left后,配合iview下拉框,会出现闪烁 引起原因:下拉图标的反转动画 目前解决方案: 添加一个全局样式: 禁用动画,其他地方也是如此, 影响控件有:gz-tr ...

  2. Linux基本命令 文件处理命令

    概述 命令格式:命令 [-选项] [参数] 例如:ls -la /etc 说明:1.个别命令使用不遵守此格式.2. 当有多个选项时,可以写在一起. ls 命令示例 文件打印命令cat.tac.more ...

  3. 《高级程序设计》8 BOM

    window对象 location对象 navigator对象 screen对象 history对象 一.window对象 BOM的核心对象是window,它表示浏览器的一个实例.在浏览器中,wind ...

  4. 【Flask】Sqlalchemy lazy

    ### 懒加载:在一对多,或者多对多的时候,如果想要获取多的这一部分的数据的时候,往往能通过一个属性就可以全部获取了.比如有一个作者,想要或者这个作者的所有文章,那么可以通过user.articles ...

  5. id和NSObject *和instanceType的区别与联系

    id 被成为万能指针,也就是可以指向任何对象. NSObject * 本身就是定义指向NSObject类型的指针. 那么这两者有什么区别吗? 这两者都是既可以作为返回值,又可以作为变量修饰.而其主要区 ...

  6. INSPIRED启示录 读书笔记 - 第28章 创业型公司的产品管理

    产品设计方式 第一步:创业初期只设三个职位,产品经理.交互设计师和原型开发人员(职位可以兼任) 第二步:快速展开产品设计(高保真原型),邀请真实的目标用户验证产品原型,迭代修改 第三步:随着迭代的深入 ...

  7. INSPIRED启示录 读书笔记 - 第14章 产品评审团

    制定更及时.更可靠的产品决策 制定决策通常是既耗时又费力的,产品公司需要一套机制让决策者和相关人员及时作出明智的产品决策.成立产品评审团是最好的解决途径 组织产品评审团的难点在于既要为高管制定产品决策 ...

  8. Struts2的<s:date>标签使用详解[转]

    作用:用来格式化显示日期的格式. 它可以用一种你指定的格式来显示 (如:“yyyy-MM-dd”),可以生成通俗易懂的注释(如:in 2 hours,14 minutes),或者用预先定义的一个格式来 ...

  9. 根据mysql中字段为时间戳查询某天数据

    //数据库保存的为时间戳select * from 表名 where FROM_UNIXTIME(存时间字段名,'%Y-%m-%d')='2017-12-12' //数据库保存的为日期格式时间SELE ...

  10. 区间dp的感悟

    学区间dp似乎也很久了...对区间dp的通用模型都了解了一些 但是做题还是很坑 上了一点难度的题基本想不出什么思路.. 目前的做题方式就是看题 想一会发现自己不会做 看题解 好巧妙啊 理解后写一发.. ...