605. Can Place Flowers【easy】

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.

错误解法:

 class Solution {
public:
bool canPlaceFlowers(vector<int>& flowerbed, int n) {
int max = ;
int temp = ; for (int i = ; i < flowerbed.size(); ++i)
{
if (flowerbed[i] == )
{
temp++;
max = temp > max ? temp : max;
}
else
{
temp = ;
}
} if ((max - ) % )
{
return ((max - ) / + >= n);
}
else
{
return ((max - ) / >= n);
}
}
};

想要用最长连续的0去求,调试了很久,但还是条件判断有问题,这种方法不妥!

参考大神们的解法,如下:

解法一:

 public class Solution {
public boolean canPlaceFlowers(int[] flowerbed, int n) {
int count = ;
for(int i = ; i < flowerbed.length && count < n; i++) {
if(flowerbed[i] == ) {
//get next and prev flower bed slot values. If i lies at the ends the next and prev are considered as 0.
int next = (i == flowerbed.length - ) ? : flowerbed[i + ];
int prev = (i == ) ? : flowerbed[i - ];
if(next == && prev == ) {
flowerbed[i] = ;
count++;
}
}
} return count == n;
}
}

解法二:

 public boolean canPlaceFlowers(int[] flowerbed, int n) {
for (int idx = ; idx < flowerbed.length && n > ; idx ++)
if (flowerbed [idx] == && (idx == || flowerbed [idx - ] == ) && (idx == flowerbed.length - || flowerbed [idx + ] == )) {
n--;
flowerbed [idx] = ;
}
return n == ;
}

解法一和解法二都需要随时更新原来数组的状态

解法三:

 class Solution {
public:
bool canPlaceFlowers(vector<int>& flowerbed, int n) {
flowerbed.insert(flowerbed.begin(),);
flowerbed.push_back();
for(int i = ; i < flowerbed.size()-; ++i)
{
if(flowerbed[i-] + flowerbed[i] + flowerbed[i+] == )
{
--n;
++i;
} }
return n <=;
}
};

不更新原来数组的值,通过多移下标来实现

605. Can Place Flowers【easy】的更多相关文章

  1. 170. Two Sum III - Data structure design【easy】

    170. Two Sum III - Data structure design[easy] Design and implement a TwoSum class. It should suppor ...

  2. 160. Intersection of Two Linked Lists【easy】

    160. Intersection of Two Linked Lists[easy] Write a program to find the node at which the intersecti ...

  3. 206. Reverse Linked List【easy】

    206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...

  4. 203. Remove Linked List Elements【easy】

    203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...

  5. 83. Remove Duplicates from Sorted List【easy】

    83. Remove Duplicates from Sorted List[easy] Given a sorted linked list, delete all duplicates such ...

  6. 21. Merge Two Sorted Lists【easy】

    21. Merge Two Sorted Lists[easy] Merge two sorted linked lists and return it as a new list. The new ...

  7. 142. Linked List Cycle II【easy】

    142. Linked List Cycle II[easy] Given a linked list, return the node where the cycle begins. If ther ...

  8. 141. Linked List Cycle【easy】

    141. Linked List Cycle[easy] Given a linked list, determine if it has a cycle in it. Follow up:Can y ...

  9. 237. Delete Node in a Linked List【easy】

    237. Delete Node in a Linked List[easy] Write a function to delete a node (except the tail) in a sin ...

随机推荐

  1. [BZOJ4650][NOI2016]优秀的拆分(SAM构建SA)

    关于解法这个讲的很清楚了,主要用了设关键点的巧妙思想. 主要想说的是一个刚学的方法:通过后缀自动机建立后缀树,再转成后缀数组. 后缀数组功能强大,但是最令人头疼的地方是模板太难背容易写错.用这个方法, ...

  2. 【枚举】URAL - 2081 - Faulty dial

    //._. ... ._. ._. ... ._. ._. ._. ._. ._. //|.| ..| ._| ._| |_| |_. |_. ..| |_| |_| //|_| ..| |_. ._ ...

  3. 杂谈PID控制算法——第一篇:三个量

    电赛训练了大半个暑假,人渐渐开始进入到了疲倦期.既然这样那就好好休息下自己也好吧. 休息也不能光休息,乘机把平常写过的一些东西好好整理也好. 从第一次训练开始我们就接触到了一个新的名词——PID控制理 ...

  4. 论文中的state-of-the-art

    最近看了几篇计算机顶会和SCI,摘要里经常出现这个词,我以为是什么算法,查阅的知是“当前最高水平”,我记得老师说不能有这种模糊词语,需要表明提高了多少,看来论文都很水,即便是IEEE,SCI.

  5. Bootstrap响应式折叠导航

    浏览器宽度缩小到一定值时,导航显示为 <nav class="navbar navbar-inverse navbar-fixed-top" role="navig ...

  6. IIS5/6/7 ASP.net 请求处理过程

    tks:http://www.cnblogs.com/TomXu/p/3756774.html 主要内容 本文讲解的是:服务器接受Http Request请求之后,是如何进入.Net CLR,从而进一 ...

  7. Delphi 7下最小化到系统托盘

    在Delphi 7下要制作系统托盘,只能制作一个比较简单的系统托盘,因为ShellAPI文件定义的TNotifyIconData结构体是比较早的版本.定义如下: 123456789   _NOTIFY ...

  8. ubuntu中apt使用以及centos中yum的使用

    centos和ubuntu是两大linux主流阵营 在centos中下载安装软件的方式 rpm rpm命令是RPM软件包的管理工具.rpm原本是Red Hat Linux发行版专门用来管理Linux各 ...

  9. Yii2 使用十二 配合ajaxFileUpload 上传文件

    1.js $("input#upload").change(function () { $.ajaxFileUpload({ url: '/members/web-members- ...

  10. NodeJS实战——创建基础应用并应用模板引擎

    本次的目的是搭建一个最基础忽地可以实现功能的NodeJSserver,可以体现出NodeJS的工作流程以及开发的基本框架. 需求:已经安装了nodejs以及express. 一.构建基础的NodeJS ...