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. SpringCloud的服务注册中心(四)- 高可用服务注册中心的搭建

    一.双 服务注册注册中心 1.服务注册中心的服务端 - EurekaServer 1.1.EurekaServer1 String.application.name=eureka-server ser ...

  2. 新概念英语(1-1)Excuse me!

    Excuse me!Whose handbag is it? A:Excuse me! B:Yes? A:Is this your handbag? B:Pardon? A:Is this your ...

  3. C++中构造函数的初始化列表(const、引用&变量初始化)

    1. 构造函数执行分为两个阶段: a.初始化阶段(初始化) 初始化阶段具体指的是用构造函数初始化列表方式来初始化类中的数据成员. ClassXX:val(a),key(b){}; b.普通计算阶段(赋 ...

  4. 谈谈自己的理解:python中闭包,闭包的实质

    闭包这个概念好难理解,身边朋友们好多都稀里糊涂的,稀里糊涂的林老冷希望写下这篇文章能够对稀里糊涂的伙伴们有一些帮助~ 请大家跟我理解一下,如果在一个函数的内部定义了另一个函数,外部的我们叫他外函数,内 ...

  5. Python之格式化输出,初始编码以及运算符

    一.题型 1.使用while循环输入 1 2 3 4 5 6  8 9 10 count = 0 while count < 10: count += 1   #count = count + ...

  6. 基于JWT标准的用户认证接口实现

    前面的话 实现用户登录认证的方式常见的有两种:一种是基于 cookie 的认证,另外一种是基于 token 的认证 .本文以基于cookie的认证为参照,详细介绍JWT标准,并实现基于该标签的用户认证 ...

  7. 27.C++- 智能指针

    智能指针 在C++库中最重要的类模板之一 智能指针实际上是将指针封装在一个类里,通过对象来管理指针. STL中的智能指针auto_ptr 头文件: <memory> 生命周期结束时,自动摧 ...

  8. wmv12下安装centos7

    第一步:安装软件: vmw版本是12,并在vmw下安装centos为CentOS-7-x86_64-DVD-1708.iso: 第二步:修改vmw虚拟网络配置 1)配置VMnet8 修改ip等信息 点 ...

  9. Spark:导入数据到oracle

    方案一: //overwrite JdbcDialect fitting for Oracle val OracleDialect = new JdbcDialect { override def c ...

  10. 卷积神经网络的一些经典网络(Lenet,AlexNet,VGG16,ResNet)

    LeNet – 5网络 网络结构为: 输入图像是:32x32x1的灰度图像 卷积核:5x5,stride=1 得到Conv1:28x28x6 池化层:2x2,stride=2 (池化之后再经过激活函数 ...