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

给定一个花坛(表示为一个数组包含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. tcp为什么要三次握手,四次挥手

    tcp为什么要三次握手,tcp为什么可靠. 为什么不能两次握手:(防止已失效的连接请求又传送到服务器端,因而产生错误) 假设改为两次握手,client端发送的一个连接请求在服务器滞留了,这个连接请求是 ...

  2. PAT甲级——A1081 Rational Sum

    Given N rational numbers in the form numerator/denominator, you are supposed to calculate their sum. ...

  3. mysql limit 偏移量过大效率解决方式 转贴

    原文地址:https://www.jianshu.com/p/f8d81df7ab28 SELECT * FROM product , ) limit SELECT * FROM product a ...

  4. Spring的自定义注解简单实现

    1.注解的示例为在方法入参上加后缀 注解代码示例: @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documente ...

  5. 搭建nodejs代理服务器,从而解决跨域问题

    先在同级处新建js文件(app.js) 使用时npm 安装 Node.js 模块语法 也就是对应的文件所在地“npm install”一下 然后安装对应需要的模块: expresspathreques ...

  6. wampserver配置服务

    搭建服务器 windows下: 安装`WampServer`软件 1.什么是WampServer: WampServer,一般称之为 WAMP ,就是Windows Apache Mysql PHP集 ...

  7. Jeecg-Boot 开发环境准备(二):开发工具安装

    目录索引: 后端开发工具 前端开发工具 Nodejs镜像 WebStorm入门配置 JeecgBoot采用前后端分离的架构,官方推荐开发工具 前端开发: Webstrom 或者 IDEA 后端开发: ...

  8. JEECMS的几个细节

    最近想自己写一些标签,看了一下JEECMS,感觉有些标签还是很值得学习的. 1.图片新闻:可以实现类似于flash切换图片的那种效果 效果: 代码: [@cms.ArtiList chnlId='' ...

  9. java填坑记录

    一.The absolute uri: [http://java.sun.com/jsp/jstl/core] cannot be resolved in either web.xml or the ...

  10. Java基础程序与面向对象

    首先,我们需要了解和知道一些Java的基本概念: 程序编译过程:.java文件会通过编译器--被编译成一个. class字节码文件---再由虚拟机运行.class文件解释运行Java程序. 编码规范: ...