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 ...
随机推荐
- [Lydsy1805月赛] 对称数
挺不错的一道数据结构题QWQ. 一开始发现这个题如果不看数据范围的话,妥妥的树上莫队啊23333,然鹅10组数据是不可能让你舒舒服服的树上莫队卡过的23333 于是想了想,这个题的模型就是,把u到v链 ...
- 3.2常用类(java学习笔记)String与StringBuffer
一.String String又称不可变字符序列. 我们看JDK源码中用于字符存储的数组有final修饰,final修饰变量就代表变量不能改变. 我们可以看API文档中对String的描述. Stri ...
- 由SequenceFile.Writer(key,value)谈toString()方法
之前有篇博客(http://www.cnblogs.com/lz3018/p/5243503.html)介绍以SequenceFile作为输入源进行矩阵乘法的过程,首先是将矩阵存储到SequenceF ...
- C#正则表达式开源工具
先交代一下背景,最近工作中经常用到正则表达式,而正则表达式这个东西我个人觉得很鸡肋,不用吧,有些功能实现起来会很麻烦.用吧,又不是说工作中经常用到,只是有时候有些需要求用到而已.但是正则表达式只要一段 ...
- JQuery提示$(...).on is not a function解决方法
版本太低了,引入较高的版本,如jquery-1.8.3.min.js
- xargs: How To Control and Use Command Line Arguments
参考: http://www.cyberciti.biz/faq/linux-unix-bsd-xargs-construct-argument-lists-utility/ http://linux ...
- ylbtech-DBD-WeShop(微店)
ylbtech-DatabaseDesgin:ylbtech-DBD-WeShop(微店) DatabaseName:WESHOP Model:微店数据设计 Type:专业技术网站 Url: 1.A, ...
- Java笔记3:Eclipse添加jar包
本文以jedis包为例,演示Eclipse如何添加和使用jar包. 1 建立一个名为ImportJarDemo的JavaProject.在该工程下建立一个libs的文件夹. 2 将下载的jedis ...
- [iOS]在NavigationController中的ScrollView中的子视图都会下移64个像素
情况是这种: 我有一个UINavigationController,设置为self.window的root视图, 然后有一个UIVIewController是UINavigtionController ...
- 获取 input 输入框的值
1.使用原生javascript: 方法一: <html> <head> <script language="javascript"> func ...