LeetCode Can Place Flowers
来源:互联网 发布:c语言中多个if else 编辑:程序博客网 时间:2024/06/05 00:44
Problem:
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 numbern, 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 = 1Output: True
Example 2:
Input: flowerbed = [1,0,0,0,1], n = 2Output: 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.
Code:
class Solution {public:bool canPlaceFlowers(vector<int>& flowerbed, int n) {if (n == 0)return true;//flowerbed为空if (flowerbed.size() == 0)return false;//flowerbed只有一个坑if (flowerbed.size() == 1){return flowerbed[0] == 0;}//处理两个特殊的点,即两个边界点if (flowerbed[0] == 0 && flowerbed[1] == 0) {flowerbed[0] = 1;n--;}for (int i = 0; i < flowerbed.size() - 1; i++) {if (flowerbed[i] == 1) {continue;}if (flowerbed[i - 1] == 0 && flowerbed[i + 1] == 0){flowerbed[i] = 1;n--;}}//最后一个点if (flowerbed[flowerbed.size() - 2] == 0 && flowerbed[flowerbed.size() - 1] == 0){flowerbed[flowerbed.size() - 1] = 1;n--;}return n <= 0;//小于等于0表示至少能种n棵树}};
阅读全文
0 0
- leetcode[Can Place Flowers]
- leetcode-Can Place Flowers
- leetCode-Can Place Flowers
- LeetCode 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
- 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
- Life_1
- KMeans+DBSCAN小试
- Windows下使用两个jdk
- git 日常使用整理(持续更新)
- POJ2778 DNA Sequence (AC自动机+矩阵快速幂)
- LeetCode Can Place Flowers
- sort函数的用法(C++排序库函数的调用) 对数组进行排序,在c++中有库函数帮我们实现,这们就不需要我们自己来编程进行排序了。 (一)为什么要用c++标准库里的排序函数 Sort()函数是c+
- vagrant 常用配置和命令
- 第一天,每天都有新的知识
- 移植uboot之修改代码支持NorFlash记录续集
- xmind的备注note中红色下划线如何去掉(转自百度)
- 第一篇文章
- MySql免安装版安装配置及安装后MySQL服务无法启动解决方案
- 视频手术刀 video bistoury(二)--ffmpeg 基石