605. Can Place Flowers【easy】
2024-09-04 13:43:59
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:
- 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.
错误解法:
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讲 什么是EMI滤波器
- VMWARE + CENTOS在windows下配置cocos2d-x android开发环境
- Android FloatingActionButton(FAB) 悬浮按钮
- 8天学通MongoDB——第二天 细说增删查改
- HTML5入门6---视频播放按钮
- AVCaptureSession 照相时获取 AVCaptureVideoPreviewLayer尺寸
- bzoj 3572: [Hnoi2014]世界树 虚树 &;&; AC500
- Ubuntu Server修改IP、DNS、hosts
- 2015-12-27 socket的用法
- vue-router项目实战总结
- 线段树(区间树)之区间染色和4n推导过程
- 22、删除链表的倒数第N个节点
- dxRangeTrackBar使用教程
- 在django中,执行原始sql语句
- springboot 多环境选择
- 百度自动推送js
- iOS开发技巧 - 使用和定制开关控件(UISwitch)
- Linux中实现多网卡绑定总结
- 【.netcore基础】MVC制器Controller依赖注入
- # 20155207王雪纯 实验一 逆向与Bof基础
热门文章
- 【树状数组】Codeforces Round #755 D. PolandBall and Polygon
- linux安装dubbo
- map泛型 map不指定泛型 与 Map<;Object,Object>;的区别
- java前后端加密(转载)
- 冒泡排序--注意flag变量的设置
- spring+activity+mysql集群
- 【sql】mysql数据库做两条数据替换的操作,不使用第三方变量
- jquery给input标签添加data-options属性
- 计算两个经纬度之间的距离(python算法)
- HTML5 Canvas 绘制库存变化折线 计算出最高最低库存