605. Can Place Flowers
2018-01-11 10:38
351 查看
1. Description
Given an array only consisting of 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 adjacent flowers.
2. Solution
把前后两端填入0,然后找到数组中所有是0,且前后均为0的位置,把这个位置变成1,并计数。
如果计数结果大于或等于n,返回true。
3. Code
Given an array only consisting of 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 adjacent flowers.
2. Solution
把前后两端填入0,然后找到数组中所有是0,且前后均为0的位置,把这个位置变成1,并计数。
如果计数结果大于或等于n,返回true。
3. Code
bool canPlaceFlowers(vector<int>& flowerbed, int n) { flowerbed.insert(flowerbed.begin(),0); flowerbed.push_back(0); int m = flowerbed.size(); int ans=0; for(int i=1;i<m-1;i++){ if(flowerbed[i]==0){ if(flowerbed[i-1]==0&&flowerbed[i+1]==0){ flowerbed[i]=1; ans++; } } } if(ans>=n) return true; else return false; }
相关文章推荐
- [LeetCode]605. Can Place Flowers
- 605. Can Place Flowers
- leetcode 605[easy]---Can Place Flowers
- [Leetcode] 605. Can Place Flowers 解题报告
- LeetCode: 605. Can Place Flowers
- 605. Can Place Flowers
- 【leetcode】605. Can Place Flowers(Python & C++)
- LeetCode 605. Can Place Flowers
- leet code 605 Can Place Flowers
- LeetCode-605:Can Place Flowers (可放置花的数量)
- 605. Can Place Flowers
- 605. Can Place Flowers
- leetcode 605. Can Place Flowers
- LeetCode 605. Can Place Flowers 自己的解法
- 605. Can Place Flowers
- [leetcode]605. Can Place Flowers
- 605 Can Place Flowers
- 605. Can Place Flowers
- leetcode 605. Can Place Flowers 间隔种花 + 补齐元素然后遍历
- 605. Can Place Flowers