605. Can Place Flowers
来源:互联网 发布:ar9271 linux驱动 编辑:程序博客网 时间:2024/05/09 17:10
题目
605. Can Place Flowers
解题思路
思路比较简单,找到一个连续3个都是空位的位置即可。
需要注意的点:
数组最首端有连续2个位置或最尾端有连续2个位置即可,不需要3个位置
注意花坛实际可容纳花的位置可能大于花的数量
具体代码
class Solution {public: bool canPlaceFlowers(vector<int>& flowerbed, int n) { if (flowerbed.size() == 1 && n <= 1) { if (!flowerbed[0]) return true; } if (!flowerbed[0] && !flowerbed[1]) { flowerbed[0] = 1; n--; } for (int i = 1; i < flowerbed.size() - 1 && n > 0; i++) { if (!flowerbed[i] && !flowerbed[i-1] && !flowerbed[i+1]) { flowerbed[i] = 1; n--; } } if (n > 0 && flowerbed.size() > 1 && !flowerbed[flowerbed.size() - 1] && !flowerbed[flowerbed.size() - 2]) { n--; } return n <= 0; }};
阅读全文
0 0
- 605. Can Place Flowers
- 605. Can Place Flowers
- 605. Can Place Flowers
- 605. Can Place Flowers
- 605. Can Place Flowers
- 605. Can Place Flowers
- 605. Can Place Flowers
- 605. Can Place Flowers
- 605. Can Place Flowers
- 605. Can Place Flowers
- [leetcode]605. Can Place Flowers
- [LeetCode]605. Can Place Flowers
- [LeetCode]605. Can Place Flowers
- LeetCode 605.Can Place Flowers
- [leetcode]: 605. Can Place Flowers
- [LeetCode] 605. Can Place Flowers
- LeetCode: 605. Can Place Flowers
- leetcode 605. Can Place Flowers
- C++11 并发指南九(综合运用: C++11 多线程下生产者消费者模型详解)
- spring boot应用
- Android 集成 EaseUi
- 300.18删除集合中的abc
- 基于ionic+cordova+angularJs
- 605. Can Place Flowers
- js string常用的函数
- radio点击选中,再次点击取消选中
- 新手之eclipse debug入门简易教程
- 微信公众平台技术文档地址
- 七牛图片瘦身
- Kerberos启动HDFS报错,java.io.FileNotFoundException: /etc/security/serverKeys/keystore.jks
- C++类简单学习(四)
- springmvc里获取application及存取值方法