Java实现 LeetCode 605 种花问题(边界问题)
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 种花问题(边界问题)的更多相关文章
- LeetCode 605. 种花问题(Can Place Flowers) 6
605. 种花问题 605. Can Place Flowers 题目描述 假设你有一个很长的花坛,一部分地块种植了花,另一部分却没有.可是,花卉不能种植在相邻的地块上,它们会争夺水源,两者都会死去. ...
- Leetcode 605.种花问题
种花问题 假设你有一个很长的花坛,一部分地块种植了花,另一部分却没有.可是,花卉不能种植在相邻的地块上,它们会争夺水源,两者都会死去. 给定一个花坛(表示为一个数组包含0和1,其中0表示没种植花,1表 ...
- 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 ...
- 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. ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- python语法学习第十一天--模块
容器----------->数据的封装 函数----------->语句的封装 类-------------->方法和属性的封装 模块----------->程序本身 导入: ...
- 设计模式之GOF23模板模式
模板模式template method 场景:具有具体流程,但具体某一步的业务不同 到银行办理业务:排队取号,办理业务,给员工打分 请客吃饭:等待,点单,吃饭,结账 模板方法模式介绍:模板方法是编程常 ...
- python之导入模块的方法
一.导入标准库模块 1.第一种方式: 可以通过以下方法导入 import time 当使用时间模块的sleep方法时可以使用 time.sleep(2) 2.第二种方式: 当只想使用sleep函数时, ...
- Loadrunner中遇到Failed to connect to server[10061] connection refused错误
(1)run-time setting/browser emulation中,将simulate a new user on each iteration 选项去掉(默认是选中的). 重新运行一切正 ...
- Java 中的过滤器Filter 和拦截器 Interceptor
1.先说拦截器 Interceptor 本项目以springboot为例: 新建 InterceptorConfig package com.opendev.mystudy.MyInterceptor ...
- 【python代码】 最大流问题+最小花费问题+python(ortool库)实现
目录 基本概念 图 邻接矩阵 最大流问题 python解决最大流问题 python解决最大流最小费用问题 基本概念 图 定义: 图G(V,E)是指一个二元组(V(G),E(G)),其中: V(G)={ ...
- 走迷宫(二):在XX限制条件下,是否走得出
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1728 解题方法:BFS+访问数组vis[][]; 给你起点位置和终点位置,让你判断能不能到达,并且拐弯 ...
- python_Excel_xlwt
xlwt 创建excel,向excel写入数据,并保存数据 安装 推荐方法: 通过pip 安装,方便简洁,如下图所示: 导入 import xlrd 创建workbook(即excel) book = ...
- Mysql数值类型,小数点后保留两个零
如有不足请帮忙留言区补充谢谢~ 一,数值类型保留小数点后两个0 在存入数据时,应客户需求数值类型,比如钱数,分数等等需要精确到小数点后几位. 800存入时显示为800.00 方法:在建表时直接定义此数 ...
- Python的自定义属性访问跟描述器以及ORM模型的简单介绍
一 . 自定义属性访问 1.__getattr__ 作用:当我们访问属性的时候,如果属性不存在(出现AttrError),该方法会被触发. 2.__getattribute__ 作用:访问属性的时候, ...