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

给定一个花坛(表示为一个数组包含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. c++ STL使用

    STL标准模板库,提供一些类似java集合类的数据结构容器.比如eque.list.vector.map 等.还提供一些支持这些容器的算法和遍历容器的迭代器. 使用方法 #include <io ...

  2. PAT甲级——A1076 Forwards on Weibo

    Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may ...

  3. 使用xshell远程连接到linux

      1.检查是否安装ssh rpm -qa | grep ssh 已安装是这样 如果没有安装,则 yum install openssh* #命令安装 2.开启ssh服务 [root@localhos ...

  4. python学习笔记3.3_json解析

    一.json文件读取 源文件:exampl.json 二.json在线解析 常用网站:https://www.json.cn/  三.数据导出为json格式文件

  5. C#跨域

    //在ConfigureServices中配置 #region 跨域 var urls = "*";//Configuration["AppConfig:Cores&qu ...

  6. 从一个prismWpfMVVM的例子中学到的

    整个程序如下,从博客园一个作者看到的例子,但是对这个例子做了点修改.我觉得这个更符合MVVM模式.这个用到了prism框架,在项目中要引用Microsoft.Practices.Prism.dll 按 ...

  7. OSG在VS2008下的配置安装

    一.准备工作 下载相关的工具软件: 1, 最新版的OSG库:OpenSceneGraph-2.8.2.zip. 2, 安装源代码所需要的工具:cmake-2.6.4-win32-x86.zip 3,  ...

  8. Hadoop构架概览

    hadoop是一个开源的软件框架,是一个利用商业硬件处理和存储大型数据的软件.从下到上主要有五个主要的组成部分: 集群,是一套主机(节点)组成的.节点可以以机架划分.这个是硬件级别的构架. YARN构 ...

  9. 初识splay

    这东西都没什么板子着实让我很难受啊,只能到网上抄抄补补, 记下两个用到的博客 https://blog.csdn.net/clove_unique/article/details/50630280 h ...

  10. JS如何获取地址栏url后面的参数?

    本文不再更新,可能存在内容过时的情况,实时更新请移步我的新博客:JS如何获取地址栏url后面的参数?: 这里提供了两种获取地址栏url后面参数的方法: 方式1 传参: window.location. ...