605. Can Place Flowers种花问题【leetcode】
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 nnew 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:
- 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.
枚举法写出来 答案,计算可以种花总数是否比n大即可,有投机的成分
//如果这个东西是奇偶数,如果里面的客房偶数比b大即可
/*
左为0 当前为0 且右边为0或边界 这时改为1 count++
左为0 当前为1 继续
左为1 当前为0 继续
左为1 当前为1 继续
*/
public class Solution {
public boolean canPlaceFlowers(int[] flowerbed, int n) {
int count =0;
int len=flowerbed.length; if(len==1&&flowerbed[0]==0){
count++;
}
if(len==2&&(flowerbed[0]==0&&flowerbed[1]==0)){
count++;
}
for(int i=1;len>1&&i<len-1;i++){
if(i==1&&flowerbed[i]==0&&flowerbed[i-1]==0){
flowerbed[i-1] =1;
count++;
}
if((flowerbed[i]==0)&&(flowerbed[i]==flowerbed[i-1])&&(flowerbed[i]==flowerbed[i+1])){
flowerbed[i]=1;
count++;
}
if(i==len-2&&flowerbed[i]==0&&flowerbed[i+1]==0){
flowerbed[i+1] =1;
count++;
}
}
if(count>=n)
{return true;}
return false;
}
}
第二种办法,根据一次写的边界值进行归类
因为只需要判断当前元素如果是0的时候与左、右边界值(如果存在着)进行对比
//如果这个东西是奇偶数,如果里面的客房偶数比b大即可
/*
左为0 当前为0 且右边为0或边界 这时改为1 count++
左为0 当前为1 继续
左为1 当前为0 继续
左为1 当前为1 继续
*/
public class Solution {
public boolean canPlaceFlowers(int[] flowerbed, int n) {
int count =0;
int len=flowerbed.length;
//boolean flag =flase;
int right=-1;
int left =-1;
for(int i=0;i<len;i++){
//判断当前是否为0,如果为0,且右边有值则比较 否则直接改
int mid=flowerbed[i]; if(mid==0){
//右边界
if(i==0){
if(i+1<len){
right =flowerbed[i+1];
if(mid==right){
flowerbed[i]=1;
count++;
} }
else {
flowerbed[i]=1;
count++;
} }
//右边界
if(i==len-1){
if(i-1>=0){
left =flowerbed[i-1];
if(mid==left){
flowerbed[i]=1;
count++;
}
}
else {
flowerbed[i]=1;
count++;
}
}
//中间值
if(i-1>=0&&i+1<len){
left =flowerbed[i-1];
right =flowerbed[i+1];
if(left==mid&&mid==right){
flowerbed[i]=1;
count++;
}
} } }
if(count>=n)
{return true;}
return false;
}
}
官方答案1
public class Solution {
public boolean canPlaceFlowers(int[] flowerbed, int n) {
int i = 0, count = 0;
while (i < flowerbed.length) {
if (flowerbed[i] == 0 && (i == 0 || flowerbed[i - 1] == 0) && (i == flowerbed.length - 1 || flowerbed[i + 1] == 0)) {
flowerbed[i++] = 1;
count++;
}
if(count>=n)
return true;
i++;
}
return false;
}
}
T :o(n) Space:O(1)
精简的写法
public class Solution {
public boolean canPlaceFlowers(int[] flowerbed, int n) {
int count =0;
int len=flowerbed.length; for(int i=0;i<len;i++){
//判断当前是否为0,如果为0,且左右相邻均为0则可以种花,种花后修改花盆属性和种花数量
if(flowerbed[i]==0){
int right=(i==len-1)?0:flowerbed[i+1];
int left=(i==0)?0:flowerbed[i-1];
if(left==right&&right==0){
flowerbed[i]=1;
count++;
}
}
if(count>=n){
return true;
}
} return false;
}
}
605. Can Place Flowers种花问题【leetcode】的更多相关文章
- 605. Can Place Flowers【easy】
605. Can Place Flowers[easy] Suppose you have a long flowerbed in which some of the plots are plante ...
- 【Leetcode_easy】605. Can Place Flowers
problem 605. Can Place Flowers 题意: solution1: 先通过简单的例子(比如000)发现,通过计算连续0的个数,然后直接算出能放花的个数,就必须要对边界进行处理, ...
- LeetCode 605. Can Place Flowers (可以种花)
Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, ...
- 【LeetCode】605. Can Place Flowers 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 贪婪算法 日期 题目地址:https://leetcode.c ...
- 605. Can Place Flowers零一间隔种花
[抄题]: Suppose you have a long flowerbed in which some of the plots are planted and some are not. How ...
- 【Leetcode】605. Can Place Flowers
Description Suppose you have a long flowerbed in which some of the plots are planted and some are no ...
- 605. Can Place Flowers
Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, ...
- Leetcode605.Can Place Flowers种花问题
假设你有一个很长的花坛,一部分地块种植了花,另一部分却没有.可是,花卉不能种植在相邻的地块上,它们会争夺水源,两者都会死去. 给定一个花坛(表示为一个数组包含0和1,其中0表示没种植花,1表示种植了花 ...
- LeetCode 605. 种花问题(Can Place Flowers) 6
605. 种花问题 605. Can Place Flowers 题目描述 假设你有一个很长的花坛,一部分地块种植了花,另一部分却没有.可是,花卉不能种植在相邻的地块上,它们会争夺水源,两者都会死去. ...
随机推荐
- Windows7下pip源配置修改
以下列举三种方式的pip源配置: 1. 设置环境变量PIP_CONFIG_FILE指向pip.ini源配置文件,pip.ini文件内容如下: [global] index-url = http://m ...
- 【Android Developers Training】 80. 管理网络使用
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- VMware Mac OS中无法找到适应的分辨率的解决办法
使用VMware安装的Mac OS中,有时在显示器的分辨率中的选择项里面,没有对应显示的分辨率可供选择的时候(无法自适应),可以在虚拟机设置里,显示器中修改强制分辨率.修改过后重启虚拟机,就可以有对应 ...
- vijos1022题解
Victoria是一位颇有成就的艺术家,他因油画作品<我爱北京天安门>闻名于世界.现在,他为了报答帮助他的同行们,准备开一个舞会. Victoria准备邀请n个已经确定的人,可是问题来了: ...
- DELPHI下的SOCK编程
DELPHI下的SOCK编程(转自http://www.cnblogs.com/devcjq/articles/2325600.html) 本文是写给公司新来的程序员的,算是一点培训的教材.本文不会 ...
- Linux 教程 技巧集
Linux 终端操作技巧 CTRL + U - 剪切光标前的内容 CTRL + K - 剪切光标至行末的内容 CTRL + Y - 粘贴 CTRL + E - 移动光标到行末 CTRL + A - 移 ...
- asp.net Mvc 动态创建Controller
有这么个需求,Urls如下: http://localhost:52804 http://localhost:52804/home/test http://localhost:52804/test1 ...
- java多线程系列(二)
对象变量的并发访问 前言:本系列将从零开始讲解java多线程相关的技术,内容参考于<java多线程核心技术>与<java并发编程实战>等相关资料,希望站在巨人的肩膀上,再通过我 ...
- jvm005 从jvm的角度谈谈线程的实现
一.线程的实现 在谈谈线程之前,我们要先知道线程是何物?在学习操作系统时,我们得知进程和线程的概念,接下来我们将开始揭示线程. 什么是进程?通过任务管理器我们就看到了进程的存在.而通过观察,我们发现只 ...
- poj 1161 Walls
https://vjudge.net/problem/POJ-1161 题意:有m个区域,n个小镇,有c个人在这些小镇中,他们要去某一个区域中聚会,从一个区域到另一个区域需要穿墙,问这些人聚到一起最少 ...