假设你有一个很长的花坛,一部分地块种植了花,另一部分却没有。可是,花卉不能种植在相邻的地块上,它们会争夺水源,两者都会死去。

给定一个花坛(表示为一个数组包含0和1,其中0表示没种植花,1表示种植了花),和一个数 n 。能否在不打破种植规则的情况下种入 n 朵花?能则返回True,不能则返回False。

示例 1:

输入: flowerbed = [1,0,0,0,1], n = 1 输出: True

示例 2:

输入: flowerbed = [1,0,0,0,1], n = 2 输出: False

注意:

  1. 数组内已种好的花不会违反种植规则。
  2. 输入的数组长度范围为 [1, 20000]。
  3. n 是非负整数,且不会超过输入数组的大小。
class Solution {
public:
bool canPlaceFlowers(vector<int>& flowerbed, int n) {
int len = flowerbed.size();
int sum1 = 0;
int sum2 = 0;
for(int i = 0; i < len; i = i + 2)
{
if(i == 0 && flowerbed[i + 1] != 1 && flowerbed[i] != 1)
sum1++;
else if(i == len - 1 && flowerbed[i - 1] != 1 && flowerbed[i] != 1)
sum1++;
else if(flowerbed[i + 1] != 1 && flowerbed[i - 1] != 1 && flowerbed[i] != 1)
sum1++;
} for(int i = 1; i < len; i = i + 2)
{
if(flowerbed[i + 1] != 1 && flowerbed[i - 1] != 1 && flowerbed[i] != 1)
sum2++;
}
if(sum1 >= n || sum2 >= n)
return true;
return false;
}
};

Leetcode605.Can Place Flowers种花问题的更多相关文章

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

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

  2. English trip V1 - 5.That's Amazing! 棒极了! Teacher:Patrick Key: can or can't

    In this lesson you will learn to describe what people can do. 在本课中,您将学习如何描述人们可以做什么. STARTE drive a c ...

  3. [Swift]LeetCode605. 种花问题 | Can Place Flowers

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

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

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

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

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

  6. 605. Can Place Flowers零一间隔种花

    [抄题]: Suppose you have a long flowerbed in which some of the plots are planted and some are not. How ...

  7. CF451E Devu and Flowers (隔板法 容斥原理 Lucas定理 求逆元)

    Codeforces Round #258 (Div. 2) Devu and Flowers E. Devu and Flowers time limit per test 4 seconds me ...

  8. 线段树或树状数组---Flowers

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=4325 Description As is known to all, the blooming tim ...

  9. hdoj 4325 Flowers【线段树+离散化】

    Flowers Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

随机推荐

  1. android Serializable 和 Parcelable 区别

      android 中自定义的对象序列化的问题有两个选择一个是Parcelable,另外一个是Serializable. 一 序列化原因: 1.永久性保存对象,保存对象的字节序列到本地文件中:2.通过 ...

  2. Cesium官方教程13--Cesium和Webpack

    原文地址:https://cesiumjs.org/tutorials/cesium-and-webpack/ Cesium 和 Webpack Webpack是非常强大非常流行的JavaScript ...

  3. PAT甲级——A1070 Mooncake

    Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn Festival. Many types ...

  4. 用Axure RP7创建响应式原型教程

    自从几年前响应式技术开始应用时,创建响应式原型就成为了很多人苦恼的事情.响应式设计用一种非常优雅的方式处理为多种设备类型使用HTML和CSS编码的应用,但是提供给UX专业人士的原型工具却没有具备同样品 ...

  5. mybatis的Example的使用

    MyBatis的Mapper接口以及Example的实例函数及详解 一.mapper接口中的方法解析 mapper接口中的函数及方法 方法 功能说明 int countByExample(UserEx ...

  6. HZOI20190819模拟26题解

    题面:https://www.cnblogs.com/Juve/articles/11376806.html A. 嚎叫响彻在贪婪的厂房: 是时候学习一下map和set的用法了...... 贪心:区间 ...

  7. idea展示runDashboard的窗口

    一.idea的runDashboard打开workspace.xml文件之后,找到component为RunDashboard的节点处,然后在component标签里添加<option name ...

  8. ac68u、r8500 梅林固件扩展为一个小型 linux 系统

    事先刷 merlin 固件 1.安装 Entware 在安装之前,你需要在路由器中插入一个 U 盘,并将其格式化为 Ext3 / Ext4 格式,插上后在 /mnt/ 下应会多个 sda 设备出来 e ...

  9. 【DM642学习笔记七】缓冲区管理BufferManagement

       (参考文档:The TMS320DM642 VideoPort Mini-Driver ——3.2 Buffer Management)    认识输入/输出视频数据的缓冲区管理,有利于对视频图 ...

  10. 文件上传 - Commons FileUpload介绍

    概述 FileUpload能够以多种不同的方式使用,具体取决于应用程序的要求.在最简单的情况下,调用单个方法来解析servlet请求,然后处理解析出来的Item集合.此外也可以自定义FileUploa ...