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:

  1. The input array won't violate no-adjacent-flowers rule.
  2. The input array size is in the range of [1, 20000].
  3. \(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的更多相关文章

  1. 605. Can Place Flowers【easy】

    605. Can Place Flowers[easy] Suppose you have a long flowerbed in which some of the plots are plante ...

  2. 【Leetcode_easy】605. Can Place Flowers

    problem 605. Can Place Flowers 题意: solution1: 先通过简单的例子(比如000)发现,通过计算连续0的个数,然后直接算出能放花的个数,就必须要对边界进行处理, ...

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

    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 (可以种花)

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

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

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

  6. 【Leetcode】605. Can Place Flowers

    Description Suppose you have a long flowerbed in which some of the plots are planted and some are no ...

  7. 【LeetCode】605. Can Place Flowers 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 贪婪算法 日期 题目地址:https://leetcode.c ...

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

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

  9. LeetCode(605,581,566)

    LeetCode(605,581,566) 摘要:605盲改通过:581开始思路错误,后利用IDE修改(多重循环跳出方法):566用C语言时需要动态内存分配,并且入口参数未能完全理解,转用C++. 6 ...

随机推荐

  1. python 模拟浏览器登陆coursera

    import requests import random import string def randomString(length): return ''.join(random.choice(s ...

  2. .net 4种单例模式

    转载: https://www.cnblogs.com/dreign/archive/2012/05/08/2490212.html using System; using System.Collec ...

  3. IDEA安装和JDK的配置

    安装: 免费获取注册码:   http://idea.lanyus.com/ 将其压缩包解压后:  应用程序在bin目录下 打开之后: 选择第二个,输入刚获取的验证码: 成功. 如果没有安装JDK报错 ...

  4. Vue框架

    Vue框架 环境: windows python3.6.2 Vue的cdn: <script src="https://cdn.jsdelivr.net/npm/vue"&g ...

  5. Hadoop API:遍历文件分区目录,并根据目录下的数据进行并行提交spark任务

    hadoop api提供了一些遍历文件的api,通过该api可以实现遍历文件目录: import java.io.FileNotFoundException; import java.io.IOExc ...

  6. Apache Spark Jobs 性能调优

    当你开始编写 Apache Spark 代码或者浏览公开的 API 的时候,你会遇到各种各样术语,比如transformation,action,RDD(resilient distributed d ...

  7. spring clound微服务架构实践(1)——搭建服务注册中心

    一.创建一个空maven parent模板 1-1.新建project,选择maven 1-2.给此模板起名 1-3.此模板的保存位置,此处放入我的git项目spring-clound-learnin ...

  8. 电力 Web SCADA 工控组态编辑器

    前言 SVG 并非仅仅是一种图像格式, 由于它是一种基于 XML 的语言,也就意味着它继承了 XML 的跨平台性和可扩展性,从而在图形可重用性上迈出了一大步.如 SVG 可以内嵌于其他的 XML 文档 ...

  9. windows汇编环境配置

    原文地址 软件下载 需要的软件已经打包,包括dosbox和MASM.如果没有这两个软件可以在下面的地址下载. http://hjwblog.com/game/汇编环境.zip 点击下载 安装dosbo ...

  10. linux下使用crontab定时执行脚本

    使用crontab定时执行脚本 cron服务是一个定时执行的服务,可以通过crontab 命令添加或者编辑需要定时执行的任务: crontab –e : 修改 crontab 文件,如果文件不存在会自 ...