首页 > 分享 > 605. Can Place Flowers

605. Can Place Flowers

题目:

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可以插一朵花,但是要注意首尾为0的特殊情况

代码:

class Solution {

public:

bool canPlaceFlowers(vector<int>& flowerbed, int n) {

int i = 0;

while(i<flowerbed.size())

{

if(flowerbed[i]==0&&(i==0||flowerbed[i-1]==0)&&(i==flowerbed.size()-1||flowerbed[i+1]==0))

{

n--;

flowerbed[i] = 1;

}

i++;

}

return n<1?true:false;

}

};

相关知识

将会种植更多的鲜花来美化城市 的翻译是:More flowers will be planted to beautify the city 中文翻译英文意思,翻译英语
雅思口语Part 1答题模板:花鸟鱼虫
flowers的意思
你可以足不出户订购全球任一国家任意地区的鲜花、牛奶、餐点等,并直接送货上门; 的翻译是:You can be ordered without having any country any region in the world, such as flowers, milk, food, and deliver it directly; 中文翻译英文意思,翻译英语
问展丨2019深圳花展——容器花园展、创意花园展、花艺展
Leetcode刷题笔记 605. 种花问题
治愈全网!小伙用羊毛毡“小花”修补城市街角,网友:好温暖的补丁……
Realize one's ambition
children can explore the plants' need for water by growing flowers and vegetables and watering them with eyedroppers, 的翻译是:儿童可以由种植鲜花和蔬菜和浇水使用吸管工具,探索植物对水的需求 中文翻译英文意思,翻译英语
新浪教育

网址: 605. Can Place Flowers https://m.huajiangbk.com/newsview212257.html

所属分类:花卉
上一篇: 牡丹广场东西花坛的绿化考虑升级一
下一篇: 《星际锦鲤养包子》