Leetcode605.Can Place Flowers种花问题
假设你有一个很长的花坛,一部分地块种植了花,另一部分却没有。可是,花卉不能种植在相邻的地块上,它们会争夺水源,两者都会死去。
给定一个花坛(表示为一个数组包含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, 20000]。
- 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种花问题的更多相关文章
- 605. Can Place Flowers种花问题【leetcode】
Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, ...
- 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 ...
- [Swift]LeetCode605. 种花问题 | Can Place Flowers
Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, ...
- LeetCode 605. 种花问题(Can Place Flowers) 6
605. 种花问题 605. Can Place Flowers 题目描述 假设你有一个很长的花坛,一部分地块种植了花,另一部分却没有.可是,花卉不能种植在相邻的地块上,它们会争夺水源,两者都会死去. ...
- LeetCode 605. Can Place Flowers (可以种花)
Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, ...
- 605. Can Place Flowers零一间隔种花
[抄题]: Suppose you have a long flowerbed in which some of the plots are planted and some are not. How ...
- CF451E Devu and Flowers (隔板法 容斥原理 Lucas定理 求逆元)
Codeforces Round #258 (Div. 2) Devu and Flowers E. Devu and Flowers time limit per test 4 seconds me ...
- 线段树或树状数组---Flowers
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=4325 Description As is known to all, the blooming tim ...
- hdoj 4325 Flowers【线段树+离散化】
Flowers Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
随机推荐
- centos7 安装 python3
sudo yum -y groupinstall "Development tools" sudo yum -y install zlib-devel bzip2-devel op ...
- eclipse安装m2e
Installation You can install last M2Eclipse release by using the following update site from within E ...
- 定时ping取返回值并绘图
figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...
- 【LGP4705】玩游戏
题目 显然这个题的期望就是逗你玩的,我们算出来总贡献除以\(nm\)就好了 设\(ans_t=\sum_{i=1}^n\sum_{j=1}^n(a_i+b_j)^t\) 二项式定理展开一下 \[ans ...
- drools跳转出现错误问题(400)
400 Sorry, a technical error occurred. Please contact a system administrator. 今天drools的管理平台tomcat部署完 ...
- Mybatis的插件 PageHelper 分页查询使用方法
参考:https://blog.csdn.net/ckc_666/article/details/79257028 Mybatis的一个插件,PageHelper,非常方便mybatis分页查询,国内 ...
- COCI2014/2015 Contest#1 D MAFIJA【基环树最大独立点集】
T1725 天黑请闭眼 Online Judge:COCI2014/2015 Contest#1 D MAFIJA(原题) Label:基环树,断环+树形Dp,贪心+拓扑 题目描述 最近天黑请闭眼在 ...
- Luogu P4933 大师(dp)
P4933 大师 题意 题目背景 建筑大师最近在跟着数学大师ljt12138学数学,今天他学了等差数列,ljt12138决定给他留一道练习题. 题目描述 ljt12138首先建了\(n\)个特斯拉电磁 ...
- 跟我一起在ubuntu中安装docker
卸载旧版本 $ sudo apt-get remove docker docker-engine docker.io 查看ubuntu版本 设置安装源 通过如下步骤,设置安装源仓库,这里我们使用阿里源 ...
- 关于Canvas的坐标系
注意Canvas的坐标系应该是这样子的: 看下面的例子: 最后的显示效果是: