LeetCode Can Place Flowers

时间:2023-01-09 15:11:14

原题链接在这里:https://leetcode.com/problems/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:

  1. The input array won't violate no-adjacent-flowers rule.
  2. The input array size is in the range of [1, 20000].
  3. n is a non-negative integer which won't exceed the input array size.

题解:

当前值为0时看前一个和后一个是否同时为0, 若是就可以栽花. 注意首尾特殊情况.

Time Complexity: O(flowerbed.length).

Space: O(1).

AC Java:

 class Solution {
public boolean canPlaceFlowers(int[] flowerbed, int n) {
if(flowerbed == null){
throw new IllegalArgumentException("Input array is null.");
} int count = 0;
for(int i = 0; i<flowerbed.length && count<n; i++){
if(flowerbed[i] == 0){
int pre = (i == 0) ? 0 : flowerbed[i-1];
int next = (i == flowerbed.length-1) ? 0 : flowerbed[i+1];
if(pre == 0 && next == 0){
count++;
flowerbed[i] = 1;
}
}
}
return count == n;
}
}

LeetCode Can Place Flowers的更多相关文章

  1. &lbrack;LeetCode&rsqb; Can Place Flowers 可以放置花

    Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, ...

  2. LeetCode 605&period; Can Place Flowers (可以种花)

    Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, ...

  3. LeetCode 605&period; 种花问题&lpar;Can Place Flowers&rpar; 6

    605. 种花问题 605. Can Place Flowers 题目描述 假设你有一个很长的花坛,一部分地块种植了花,另一部分却没有.可是,花卉不能种植在相邻的地块上,它们会争夺水源,两者都会死去. ...

  4. 【LeetCode】605&period; Can Place Flowers 解题报告(Python & C&plus;&plus;)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 贪婪算法 日期 题目地址:https://leetcode.c ...

  5. 605&period; Can Place Flowers种花问题【leetcode】

    Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, ...

  6. LeetCode算法题-Can Place Flowers(Java实现)

    这是悦乐书的第272次更新,第287篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第140题(顺位题号是605).假设你有一个花坛,其中一些地块是种植的,有些则不是. 然 ...

  7. 【Leetcode】605&period; Can Place Flowers

    Description Suppose you have a long flowerbed in which some of the plots are planted and some are no ...

  8. C&num;LeetCode刷题之&num;605-种花问题( Can Place Flowers)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3724 访问. 假设你有一个很长的花坛,一部分地块种植了花,另一部 ...

  9. LeetCode第四天

    leetcode 第四天 2018年1月4日 15.(628)Maximum Product of Three Numbers JAVA class Solution { public int max ...

随机推荐

  1. Unity3D代码旋转

    subGameObject.transform.Rotate (,,,Space.Self); 一行代码

  2. 忽然发现,if语句没有相应的continue功能

    就是剩下部分语句不用执行了,但是又不退出当前函数,只退出当前if块.虽说else可以解决问题,但是这样还是会重复写代码,假如continue语句后面的内容是相同的话.当然可以通过再次加一个if语句解决 ...

  3. hdu3278Puzzle

    其实最终的结果无非是中间8个方块是相同的颜色,外面16个方块是另外两种颜色.那么其实可以把外面两种颜色看作是0,中间的颜色看做是1. 那么题目就变成了把那种颜色看做1,而其它两种颜色看做0,可以用最少 ...

  4. 检索字符创 php

    strstr()可以返回匹配的值 echo strstr("localhost", "os");返回ost echo substr_count("gg ...

  5. Kafka 在行动:7步实现从RDBMS到Hadoop的实时流传输

    原文:https://coyee.com/article/11095-kafka-in-action-7-steps-to-real-time-streaming-from-rdbms-to-hado ...

  6. UOJ&num;291&period; 【ZJOI2017】树状数组 树套树

    原文链接https://www.cnblogs.com/zhouzhendong/p/UOJ291.html 题解 结论:这个写错的树状数组支持的是后缀加和后缀求和.这里的后缀求和在 x = 0 的时 ...

  7. GIL学习

    GIL锁 一.GIL的简单概述 二.GIL对于多线程的影响 三.解决GIL对于多线程影响的方案 回到顶部 一.GIL的简单概述 1.概念 GIL ( Global Interperter Lock ) ...

  8. &period;NET代码混淆——开源&period;net 混淆器ConfuserEx介绍

    转载:https://blog.csdn.net/xiaoyong_net/article/details/78988264

  9. 【codevs2205】等差数列

    题目大意:给定一个长度为 N 的序列,求这个序列中等差数列的个数. 题解:根据题意应该是一道序列计数 dp.设 \(dp[i][j]\) 表示以第 i 项结尾,公差为 j 的等差数列的个数,则状态转移 ...

  10. python 杂谈

    python 当前文件导入自定义模块的时候,会默认执行一遍 python使用的变量必须是已经定义或者声明过的.