605. 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:
- The input array won't violate no-adjacent-flowers rule.
- The input array size is in the range of [1, 20000].
- \(n\) is a non-negative integer which won't exceed the input array size.
自己代码:
\(n\leq3\) 时, 情况都列出来;
\(n>3\) 时, 那点若为首点 i == 0 && A[i] == 0 && A[1] == 0,则count+1,且将A[0]=1, 否则那点若为尾点i == (n - 1) && A[i] == 0 && A[i - 1] == 0,则count+1,且将A[n-1]=1,否则的话,这个点既不是首也不是尾巴,就可以放心的看这个点和前一个点以及后一个点是否全为0,A[i] == 0 && A[i - 1] == 0 && A[i + 1] == 0,若是则count+1,且A[i]=1.
我这个逻辑稍显复杂,较难控制.
编写这个用了多久怎么能和你说呢!
我用了足足1分钟! ^^
\(O(n)\) time, \(O(1)\) extra space.
bool canPlaceFlowers(vector<int>& A, int k) {
int i, count = 0, n = A.size();
if (n <= 3) {
if (n == 1 && A[0] == 0) count = 1;
if (n == 1 && A[0] == 1) count = 0;
if (n == 2 && A[0] == 0 && A[1] == 0) count = 1;
if ((n == 2 && A[0] == 1 && A[1] == 0) || (n == 2 && A[0] == 0 && A[1] == 1)) count = 0;
if (n == 3 && A[0] == 0 && A[1] == 0 && A[2] == 0) count = 2;
if (n == 3 && A[0] == 0 && A[1] == 0 && A[2] == 1) count = 1;
if (n == 3 && A[0] == 1 && A[1] == 0 && A[2] == 0) count = 1;
return k <= count ? true : false;
}
else {
for (i = 0; i < n; i++) {
if (i == 0 && A[i] == 0 && A[1] == 0) {A[i] = 1; count++;}
else if (i == (n - 1) && A[i] == 0 && A[i - 1] == 0) {A[i] = 1; count++;}
else if (A[i] == 0 && A[i - 1] == 0 && A[i + 1] == 0) {A[i] = 1; count++;}
}
return k <= count ? true : false;
}
}
再看看人家的,妈的过分吧,是人脑袋吗,还有点正常人的思维吗,想杀人!
\(O(n)\) time, \(O(1)\) extra space.
bool canPlaceFlowers(vector<int>& A, int n) {
A.insert(A.begin(), 0);
A.push_back(0);
for (int i = 1; i < A.size() - 1; ++i){
if (A[i - 1] + A[i] + A[i + 1] == 0){
--n;
++i;
}
}
return n <= 0;
}
605. Can Place Flowers的更多相关文章
- 605. Can Place Flowers【easy】
605. Can Place Flowers[easy] Suppose you have a long flowerbed in which some of the plots are plante ...
- 【Leetcode_easy】605. Can Place Flowers
problem 605. Can Place Flowers 题意: solution1: 先通过简单的例子(比如000)发现,通过计算连续0的个数,然后直接算出能放花的个数,就必须要对边界进行处理, ...
- 605. Can Place Flowers种花问题【leetcode】
Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, ...
- 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 ...
- 【Leetcode】605. Can Place Flowers
Description Suppose you have a long flowerbed in which some of the plots are planted and some are no ...
- 【LeetCode】605. Can Place Flowers 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 贪婪算法 日期 题目地址:https://leetcode.c ...
- LeetCode 605. 种花问题(Can Place Flowers) 6
605. 种花问题 605. Can Place Flowers 题目描述 假设你有一个很长的花坛,一部分地块种植了花,另一部分却没有.可是,花卉不能种植在相邻的地块上,它们会争夺水源,两者都会死去. ...
- LeetCode(605,581,566)
LeetCode(605,581,566) 摘要:605盲改通过:581开始思路错误,后利用IDE修改(多重循环跳出方法):566用C语言时需要动态内存分配,并且入口参数未能完全理解,转用C++. 6 ...
随机推荐
- restful架构风格设计准则(一)以资源为中心、自描述的请求响应、资源状态迁移为粒度
读书笔记,原文链接:http://www.cnblogs.com/loveis715/p/4669091.html,感谢作者! 一.需求描述 当用户在某个电子商务网站购物时,他首先查看要购买的商品分类 ...
- CentOS7 安装eclipse
1. 首先将eclipse的压缩包文件解压到/opt目录下,要使用root权限.执行如下解压命令:tar -zxvf eclipse-jee-oxygen-1a-linux-gtk-x86_64.ta ...
- 使用TortoiseSVN打Tag
参考了 https://blog.csdn.net/liuzx32/article/details/9123401. 值得注意的点是: 选择路径的时候,不要先点进去自己建好叶子节点路径再选择该路径,会 ...
- 算法 排序NB二人组 堆排序 归并排序
参考博客:基于python的七种经典排序算法 常用排序算法总结(一) 序前传 - 树与二叉树 树是一种很常见的非线性的数据结构,称为树形结构,简称树.所谓数据结构就是一组数据的集合连同它们的储 ...
- Vue项目结构说明
简单介绍目录结构 http://blog.csdn.net/u013778905/article/details/53864289 (别人家的链接,留给我自己看的)
- Java入门1
一.eclipse的简单使用 1.新建项目 在package explorer的空白处点击右键,新建一个项目(new->Java Project)或者点击菜单栏的File->JavaPro ...
- 使Asp.net Core同时支持输出Json/Xml
我们知道Asp.net Core是支持输出为Json格式的.同时也支持输出为xml格式.只要我们正确的配置.并在Request时指定正确的Accept,即可根据不同的Header来输出不同的格式. 前 ...
- 初始Spring MVC——练手小项目
初始Spring MVC 前几天开始了我的spring学习之旅,由于之前使用过MVC模式来做项目,所以我先下手的是 Spring MVC,做个练手项目,非常简单 项目介绍: 用户输入信息 -> ...
- Linux之centos系统常用命令总结
1.查看系统版本 cat /etc/redhat-release 文件与目录操作 命令 解析 cd /home 进入 '/home' 目录 cd .. 返回上一级目录 cd ../.. 返回上两级目录 ...
- ASP.NET Core + Docker + Jenkins + gogs + CentOS 从零开始搭建持续集成
为什么不用gitlab? 没有采用gitlab,因为gitlab比较吃配置,至少得2核4G的配置.采用go语言开发的gogs来代替,搭建方便(不到10分钟就能安装完成),资源消耗低,功能也比较强大,也 ...