【LeetCode】605. Can Place Flowers 解题记录
问题描述
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.
Given an integer array flowerbed containing 0’s and 1’s, where 0 means empty and 1 means not empty, and an integer n, return if n new flowers can be planted in the flowerbed without violating the no-adjacent-flowers rule.
测试样例
1 | Input: flowerbed = [1,0,0,0,1], n = 1 |
1 | Input: flowerbed = [1,0,0,0,1], n = 2 |
说明
1 | 1 <= flowerbed.length <= 2 * 10^4 |
解题
思路
遍历 flower bed 只要遇到可以放入的位置,放入即可
补充:
- 注意处理第一个和最后一个位置
- 时间复杂度
O(n)
代码
1 | class Solution { |
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 哆啦 C 梦!
评论