//最近在忙本科毕设,跨专业伤不起!!
//本题国服通过率20%,外服30%
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.
中文题目:
假设你有一个很长的花坛,一部分地块种植了花,另一部分却没有。可是,花卉不能种植在相邻的地块上,它们会争夺水源,两者都会死去。
给定一个花坛(表示为一个数组包含0和1,其中0表示没种植花,1表示种植了花),和一个数 n 。能否在不打破种植规则的情况下种入 n 朵花?能则返回True,不能则返回False。
示例 1:
输入: flowerbed = [1,0,0,0,1], n = 1 输出: True
示例 2:
输入: flowerbed = [1,0,0,0,1], n = 2 输出: False
注意:
- 数组内已种好的花不会违反种植规则。
- 输入的数组长度范围为 [1, 20000]。
- n 是非负整数,且不会超过输入数组的大小。
code1:
class Solution {
public:
bool canPlaceFlowers(vector<int>& flowerbed, int n) {
//按照题目要求,用最原始的方法,一个一个摆,因为不管什么方法都要遍历
int cnt=0;
int sum=0;
int right=0;
int k=0;
for(auto &num:flowerbed)
++right;
for(auto &num: flowerbed){
if(cnt>0){
if(cnt==right-1)
break;
if(flowerbed[cnt]==0&&flowerbed[cnt-1]!=1&&flowerbed[cnt+1]!=1){
if(right==cnt)
k=1;
flowerbed[cnt]=1;
++sum;
}
}
if(cnt==0){
if(flowerbed[cnt]==0&&flowerbed[cnt+1]!=1){
flowerbed[cnt]=1;
++sum;
}
}
++cnt;
}
if(flowerbed[cnt]!=1&&flowerbed[cnt-1]!=1)
++sum;
if(sum>=n)
return 1;
return 0;
}
};
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//优化提高运行速度,5_18,直接使用size函数
class Solution {
public:
bool canPlaceFlowers(vector<int>& flowerbed, int n) {
//按照题目要求,用最原始的方法,一个一个摆,因为不管什么方法都要遍历
int cnt=0;
int sum=0;
auto right=flowerbed.size();
int k=0;
for(auto &num: flowerbed){
if(cnt>0){
if(cnt==right-1)
break;
if(flowerbed[cnt]==0&&flowerbed[cnt-1]!=1&&flowerbed[cnt+1]!=1){
if(right==cnt)
k=1;
flowerbed[cnt]=1;
++sum;
}
}
if(cnt==0){
if(flowerbed[cnt]==0&&flowerbed[cnt+1]!=1){
flowerbed[cnt]=1;
++sum;
}
}
++cnt;
}
if(flowerbed[cnt]!=1&&flowerbed[cnt-1]!=1)
++sum;
if(sum>=n)
return 1;
return 0;
}
code3:使用迭代器
class Solution {
public:
bool canPlaceFlowers(vector<int>& flowerbed, int n) {
auto b=flowerbed.begin(),e=flowerbed.end();
auto temp=flowerbed.begin();
int cnt=0;
for(b;b!=e;++b){
if(b==temp){//当位于第一个元素时
if(*b!=1&&*(b+1)!=1){
*b=1;
++cnt;
}
}
else{//不位于第一个元素时
if(*b!=1){//当这个位置没有种花时,判断是否往里面种花
if(*(b+1)!=1&&*(b-1)!=1){
*b=1;
++cnt;
}
}
}
if(b==(e-1)){//b指向最后一个元素时
if(*(e-2)!=1&&*(e-1)!=1){
*(e-1)=1;
++cnt;
}
}
}
if(cnt>=n)
return true;
return false;
}
};