LeetCode刷题记录8——605. Can Place Flowers(easy)
LeetCode刷题记录8——605. Can Place Flowers(easy)
目录
LeetCode刷题记录8——605. Can Place Flowers(easy)
题目
语言
思路
源码
后记
题目
题目说给定一个数组,数组中只有0或1,1代表此处种了花,0代表此处空闲不种花。种花的规则是相邻之间不能种花,只能隔一下种一个。给定一个整数n,代表这个数组还能种多少多花,如果能种的下n朵,就返回true;否则返回false。
语言
Java、C++(算法用的一模一样,只是换了一种语言)
思路
整体思路:遍历整个数组,发现能种花的地方,用count累加计数。
大体先分两种情况:
如果n=0,那么肯定返回true,因为种0朵当然能种下。
当n不为0时:
如果数组长度为0,则返回false
如果数组长度为1,并且这个值为1,返回false;否则返回true
如果数组长度大于1:
考虑开头:i=0,如果下标为0和1的值均不为1,则count++,并且值置为1
考虑结尾:i=length-1,如果下标length-1和length-2的值均不为1,count++,并且值置为1
剩余情况:当这个值不为1,且前一个和后一个均不为1时,count++,并且值置为1
最终将count与输入的n对比,如果count>=n,则返回true;否则返回false。
源码
class Solution {public boolean canPlaceFlowers(int[] flowerbed, int n) {if(n==0)return true;else {int count=0;if(flowerbed.length==0) return false;else if(flowerbed.length==1) {if(flowerbed[0]==1) return false;else return true;}else {for(int i=0;i<flowerbed.length;i++) {if(i==0)if(flowerbed[i]!=1&&flowerbed[i+1]!=1) {count++;flowerbed[i]=1;}if(i==flowerbed.length-1)if(flowerbed[i-1]!=1&&flowerbed[i]!=1) {count++;flowerbed[i]=1;}if(i!=0&&i!=flowerbed.length-1) {if(flowerbed[i]!=1&&flowerbed[i-1]!=1&&flowerbed[i+1]!=1) {count++;flowerbed[i]=1;}}}if(count>=n)return true;elsereturn false;}}}
}
后记
其实做这题的主要是要看懂题目中的adjacent 是啥意思,这是相邻的意思,如果这个理解错了,后面就凉凉。
LeetCode刷题记录8——605. Can Place Flowers(easy)相关推荐
- LeetCode刷题记录13——705. Design HashSet(easy)
LeetCode刷题记录13--705. Design HashSet(easy) 目录 LeetCode刷题记录13--705. Design HashSet(easy) 前言 题目 语言 思路 源 ...
- LeetCode刷题记录11——290. Word Pattern(easy)
LeetCode刷题记录11--290. Word Pattern(easy) 目录 LeetCode刷题记录11--290. Word Pattern(easy) 题目 语言 思路 源码 后记 题目 ...
- LeetCode刷题记录7——824. Goat Latin(easy)
LeetCode刷题记录7--824. Goat Latin(easy) 目录 LeetCode刷题记录7--824. Goat Latin(easy) 题目 语言 思路 后记 题目 题目需要将一个输 ...
- LeetCode刷题记录5——441. Arranging Coins(easy)
LeetCode刷题记录5--441. Arranging Coins(easy) 目录 LeetCode刷题记录5--441. Arranging Coins(easy) 题目 语言 思路 后记 题 ...
- LeetCode刷题记录4——67. Add Binary(easy)
LeetCode刷题记录4--67. Add Binary(easy) 目录 LeetCode刷题记录4--67. Add Binary(easy) 题目 语言 思路 后记 题目 今天这题是与字符串相 ...
- LeetCode刷题记录2——217. Contains Duplicate(easy)
LeetCode刷题记录2--217. Contains Duplicate(easy) 目录 LeetCode刷题记录2--217. Contains Duplicate(easy) 题目 语言 思 ...
- LeetCode刷题记录15——21. Merge Two Sorted Lists(easy)
LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) 目录 LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) ...
- LeetCode刷题记录14——257. Binary Tree Paths(easy)
LeetCode刷题记录14--257. Binary Tree Paths(easy) 目录 前言 题目 语言 思路 源码 后记 前言 数据结构感觉理论简单,实践起来很困难. 题目 给定一个二叉树, ...
- LeetCode刷题记录12——232. Implement Queue using Stacks(easy)
LeetCode刷题记录12--232. Implement Queue using Stacks(easy) 目录 LeetCode刷题记录12--232. Implement Queue usin ...
最新文章
- php auth_http,php auth_http类库进行身份效验
- 前方高能!金三银四Java高级工程师面试题整理
- C语言的补码表示和unsigned及signed的转换
- BAT 才不关心币价,19年的5大重点在这里!
- 《系统集成项目管理工程师》必背100个知识点-19项目管理计划的内容
- 启明云端方案分享| ESP32-S2 摄像头 WIFI方案应用于智能猫眼
- 打印10以内偶数乘积的列表python_将列表中的所有偶数打印到给定的数字
- python里的关键字有哪些_Python 中的关键字有哪些?
- JDBC预处理对象prepareStatement
- html文档基本结构由哪三对,第3章 网页制作及HTML语言基本结构简介.ppt
- 【英语学习】【Python】Programming in Python 3 的目录
- java手机号分割_java 获取手机号归属地
- PostgreSQL 数据库检测到 ShareLock 死锁的处理方法
- mysql中explain使用
- java中计算两日期相减方法
- 2元一次方程计算机求解,在Excel中求解二元一次方程组
- 电工电子技术基础----multisim7使用及实验过程
- mac pem证书登陆linux,Mac通过SSH使用PEM文件登录服务器
- 模电笔记3 三极管 光电三极管
- 苹果手机备份数据到电脑什么位置 iPhone备份到电脑上的东西在哪里
热门文章
- 微信小程序wx:for 的使用
- TMS Scripter importtool的使用
- 第三次作业+105032014085
- apiCloud中的数据库操作mcm-js-sdk的使用
- 如何修改Sql2005注册服务器名称 {转载}
- 网页如何与mysql服务器建立连接不上,html与mysql建立连接数据库
- python时间序列动图_手把手教你用Python进行时间序列分解和预测
- java工具集_Java 工具集
- ESI计算机排名2021年01月,喜忧参半!2021年5月ESI世界大学排行榜,21个学科排名数据分析!...
- php语句中两个if语句,如何在PHP中组合两个IF语句