605. 种花问题

假设你有一个很长的花坛,一部分地块种植了花,另一部分却没有。可是,花卉不能种植在相邻的地块上,它们会争夺水源,两者都会死去。

给定一个花坛(表示为一个数组包含0和1,其中0表示没种植花,1表示种植了花),和一个数 n 。能否在不打破种植规则的情况下种入 n 朵花?能则返回True,不能则返回False。

示例 1:

输入: flowerbed = [1,0,0,0,1], n = 1

输出: True

示例 2:

输入: flowerbed = [1,0,0,0,1], n = 2

输出: False

注意:

数组内已种好的花不会违反种植规则。

输入的数组长度范围为 [1, 20000]。

n 是非负整数,且不会超过输入数组的大小。

PS:

边界问题处理好,在最左面和最右面都加上一个0也可以,或者直接计算

class Solution {
public boolean canPlaceFlowers(int[] flowerbed, int n) {
int count=0;
int can = 0;
int first = 0;
while(first<flowerbed.length&&flowerbed[first]==0){
first++;
count++;
}
if(first==flowerbed.length){
return (flowerbed.length+1)/2>=n;
}
can+=count/2;
count=0;
for(int i=first+1;i<flowerbed.length;i++){
if(flowerbed[i]==1){
can+=(count-1)/2;
count=0;
} else{
count++;
}
}
can+=count/2;
return can>=n;
}
}

Java实现 LeetCode 605 种花问题(边界问题)的更多相关文章

  1. LeetCode 605. 种花问题(Can Place Flowers) 6

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

  2. Leetcode 605.种花问题

    种花问题 假设你有一个很长的花坛,一部分地块种植了花,另一部分却没有.可是,花卉不能种植在相邻的地块上,它们会争夺水源,两者都会死去. 给定一个花坛(表示为一个数组包含0和1,其中0表示没种植花,1表 ...

  3. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  4. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  5. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  6. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  7. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  8. Java for LeetCode 200 Number of Islands

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  9. Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

随机推荐

  1. vs每次生成都全部编译的问题

    最近vs每次生成都会编译整个工程,经查找为.qrc中的资源路径不存在导致,删除路径后问题解决. 原文来自微信公众号"程序员成长日志",已经工作的程序员朋友可以关注下,分享日常工作中 ...

  2. 设计模式之GOF23享元模式

    享元模式FlyWeight 场景:如果有很多个完全相同或者相似的对象,可以节省内存资源 核心: 享元模式以共享的方式高效地支持大量细粒对象的重用 享元对象做到共享的关键是区分了内部状态和外部状态: 内 ...

  3. org.springframework.web.bind.annotation不存在 site:blog.csdn.net(IDEA中运行springboot时报错)

    原因:MAVEN版本与IDEA版本不兼容问题, maven虽然更新比较慢,但是最新的3.6.6在与IDEA2019版本及以下版本兼容时会出现以上问题 解决办法:重新配置一个3.6低级别版本的maven ...

  4. 科技感满满,华为云DevCloud推出网页暗黑模式

    近期,华为云DevCloud推出了暗黑模式,让用户在网页端也可以体验到桌面级应用才有的特性.   深色模式(Dark Mode),俗称暗黑模式.是近2年以来用户呼声最高的功能之一,一些国外顶级厂商都将 ...

  5. Docker之docker log详解

    1.显示所有log docker logs [OPTIONS] <CONTAINER>   #显示某个容器的所有log docker-compose logs  #显示启动的所有容器的lo ...

  6. hdu6314 容斥+数学

    题意 : n*m的矩阵 可以涂黑白两色 问至少A行B列为黑色的涂色方案种类数,答案对998244353取模,1<=n,m,A,B<=3000 题解:  ans=sum{A,..n}sum( ...

  7. 一、$HTML, HTTP,web综合问题

    1.前端需要注意哪些SEO 合理的title.description.keywords:搜索对着三项的权重逐个减小,title值强调重点即可,重要关键词出现不要超过2次,而且要靠前,不同页面title ...

  8. Java 面向对象基础

    面向对象的基础 局部变量和成员变量区别: 1)定义的位置不同    成员变量直接定义在class中    局部变量在某个{}中或者再某个方法中 2)在内存中的位置不同    对象的成员变量会在内存中的 ...

  9. 上古神器vim系列之移动三板斧

    [导读] 前文总结了vim如何进入,如何保存退出,如何进入编辑模式.本文来总结一些稍微进阶的内容,在normal模式下如何高效的浏览代码. 模式回顾 在normal模式下主要用于浏览代码,那么有哪些方 ...

  10. 看完这篇还不懂Redis的RDB持久化,你们来打我!

    一.为什么需要持久化 redis里有10gb数据,突然停电或者意外宕机了,再启动的时候10gb都没了?!所以需要持久化,宕机后再通过持久化文件将数据恢复. 二.优缺点 1.rdb文件 rdb文件都是二 ...