605. Can Place Flowers【easy】
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 <=;
}
};
不更新原来数组的值,通过多移下标来实现
605. Can Place Flowers【easy】的更多相关文章
- 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 ...
- 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 ...
- 206. Reverse Linked List【easy】
206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...
- 203. Remove Linked List Elements【easy】
203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...
- 83. Remove Duplicates from Sorted List【easy】
83. Remove Duplicates from Sorted List[easy] Given a sorted linked list, delete all duplicates such ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- POJ 1113 Wall(凸包)
[题目链接] http://poj.org/problem?id=1113 [题目大意] 给出一个城堡,要求求出距城堡距离大于L的地方建围墙将城堡围起来求所要围墙的长度 [题解] 画图易得答案为凸包的 ...
- [BZOJ1095]捉迷藏
点了动态点分治的科技树,这道题是树形态不变的动态点分治,形态变化的话...待会补 考虑点分治过程中的这样一种结构:按递归层次把当前层的重心与上层重心互相连接,这就是点分治树,容易看出它的树高只有$O( ...
- python3 开发面试题(装饰器必须考)6.4
def f(): print("2018-06-04") # 每次调用f的时候 在打印"2018-06-04" 之前打印一句 开始, 之后再打印一句 结束 de ...
- jQuery判断一个元素是否为另一个元素的子元素
判断:当前元素是否是被筛选元素的子元素 jQuery.fn.isChildOf = function(b){ return (this.parents(b).length > 0); }; 判断 ...
- iOS项目之企业证书打包和发布
一.打包ipa 个人发布证书和企业发布证书打包 app 大同小异,只是打包时导出选项不同,企业证书打包选择 Save for Enterprise Deployment ,并最终导出 ipa 包.详细 ...
- 记录一次Elasticsearch线上部署后出现:org.elasticsearch.client.transport.NoNodeAvailableException: None of the configured nodes are available: []的问题解决
说明:ES部署了3个节点,而一般情况只要这三个节点的IP其中一个都可以连接,Web端口使用的是9500,Client连接使用的是9600,调用程序使用了ES原生Client进行连接. 解决方法: 1. ...
- adobe flash builder 4.6最新能用的序列号
神key来了: 1424-4464-3877-6130-5013-5457 妈的,我两台开发机器,有一台死活激活不了,每隔一段时间就去网上搜搜激活方式已经快成为我的一个生活习惯了,可惜每次出来的都是下 ...
- MathType如何插入连乘
这个就是连乘符号,只不过看着不习惯......就在眼皮子底下.
- 扩展gridview轻松实现冻结行和列(增强型)
上一篇说过,还可以扩展gridview的分页功能以及实现导出结果为EXCEL/PDF的功能.实现好后应该封装起来,以方便后续的项目简单使用.至于要如何实现,我想不必过多的说了.下面是显示结果和主要的代 ...
- mormot支持websocket
mormot支持websocket 根据定义,HTTP连接是无状态的和单向的,也就是说,客户机向服务器发送一个请求,该服务器通过一个应答回复.没有客户端的预先请求,就没有办法让服务器发送消息给客户机. ...