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 <=;
}
};

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

最新文章

  1. [转]第1讲 什么是EMI滤波器
  2. VMWARE + CENTOS在windows下配置cocos2d-x android开发环境
  3. Android FloatingActionButton(FAB) 悬浮按钮
  4. 8天学通MongoDB——第二天 细说增删查改
  5. HTML5入门6---视频播放按钮
  6. AVCaptureSession 照相时获取 AVCaptureVideoPreviewLayer尺寸
  7. bzoj 3572: [Hnoi2014]世界树 虚树 &amp;&amp; AC500
  8. Ubuntu Server修改IP、DNS、hosts
  9. 2015-12-27 socket的用法
  10. vue-router项目实战总结
  11. 线段树(区间树)之区间染色和4n推导过程
  12. 22、删除链表的倒数第N个节点
  13. dxRangeTrackBar使用教程
  14. 在django中,执行原始sql语句
  15. springboot 多环境选择
  16. 百度自动推送js
  17. iOS开发技巧 - 使用和定制开关控件(UISwitch)
  18. Linux中实现多网卡绑定总结
  19. 【.netcore基础】MVC制器Controller依赖注入
  20. # 20155207王雪纯 实验一 逆向与Bof基础

热门文章

  1. 【树状数组】Codeforces Round #755 D. PolandBall and Polygon
  2. linux安装dubbo
  3. map泛型 map不指定泛型 与 Map&lt;Object,Object&gt;的区别
  4. java前后端加密(转载)
  5. 冒泡排序--注意flag变量的设置
  6. spring+activity+mysql集群
  7. 【sql】mysql数据库做两条数据替换的操作,不使用第三方变量
  8. jquery给input标签添加data-options属性
  9. 计算两个经纬度之间的距离(python算法)
  10. HTML5 Canvas 绘制库存变化折线 计算出最高最低库存