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累加计数。

大体先分两种情况:

  1. 如果n=0,那么肯定返回true,因为种0朵当然能种下。

  2. 当n不为0时:

    1. 如果数组长度为0,则返回false

    2. 如果数组长度为1,并且这个值为1,返回false;否则返回true

    3. 如果数组长度大于1:

      1. 考虑开头:i=0,如果下标为0和1的值均不为1,则count++,并且值置为1

      2. 考虑结尾:i=length-1,如果下标length-1和length-2的值均不为1,count++,并且值置为1

      3. 剩余情况:当这个值不为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)相关推荐

  1. LeetCode刷题记录13——705. Design HashSet(easy)

    LeetCode刷题记录13--705. Design HashSet(easy) 目录 LeetCode刷题记录13--705. Design HashSet(easy) 前言 题目 语言 思路 源 ...

  2. LeetCode刷题记录11——290. Word Pattern(easy)

    LeetCode刷题记录11--290. Word Pattern(easy) 目录 LeetCode刷题记录11--290. Word Pattern(easy) 题目 语言 思路 源码 后记 题目 ...

  3. LeetCode刷题记录7——824. Goat Latin(easy)

    LeetCode刷题记录7--824. Goat Latin(easy) 目录 LeetCode刷题记录7--824. Goat Latin(easy) 题目 语言 思路 后记 题目 题目需要将一个输 ...

  4. LeetCode刷题记录5——441. Arranging Coins(easy)

    LeetCode刷题记录5--441. Arranging Coins(easy) 目录 LeetCode刷题记录5--441. Arranging Coins(easy) 题目 语言 思路 后记 题 ...

  5. LeetCode刷题记录4——67. Add Binary(easy)

    LeetCode刷题记录4--67. Add Binary(easy) 目录 LeetCode刷题记录4--67. Add Binary(easy) 题目 语言 思路 后记 题目 今天这题是与字符串相 ...

  6. LeetCode刷题记录2——217. Contains Duplicate(easy)

    LeetCode刷题记录2--217. Contains Duplicate(easy) 目录 LeetCode刷题记录2--217. Contains Duplicate(easy) 题目 语言 思 ...

  7. LeetCode刷题记录15——21. Merge Two Sorted Lists(easy)

    LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) 目录 LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) ...

  8. LeetCode刷题记录14——257. Binary Tree Paths(easy)

    LeetCode刷题记录14--257. Binary Tree Paths(easy) 目录 前言 题目 语言 思路 源码 后记 前言 数据结构感觉理论简单,实践起来很困难. 题目 给定一个二叉树, ...

  9. LeetCode刷题记录12——232. Implement Queue using Stacks(easy)

    LeetCode刷题记录12--232. Implement Queue using Stacks(easy) 目录 LeetCode刷题记录12--232. Implement Queue usin ...

最新文章

  1. php auth_http,php auth_http类库进行身份效验
  2. 前方高能!金三银四Java高级工程师面试题整理
  3. C语言的补码表示和unsigned及signed的转换
  4. BAT 才不关心币价,19年的5大重点在这里!
  5. 《系统集成项目管理工程师》必背100个知识点-19项目管理计划的内容
  6. 启明云端方案分享| ESP32-S2 摄像头 WIFI方案应用于智能猫眼
  7. 打印10以内偶数乘积的列表python_将列表中的所有偶数打印到给定的数字
  8. python里的关键字有哪些_Python 中的关键字有哪些?
  9. JDBC预处理对象prepareStatement
  10. html文档基本结构由哪三对,第3章 网页制作及HTML语言基本结构简介.ppt
  11. 【英语学习】【Python】Programming in Python 3 的目录
  12. java手机号分割_java 获取手机号归属地
  13. PostgreSQL 数据库检测到 ShareLock 死锁的处理方法
  14. mysql中explain使用
  15. java中计算两日期相减方法
  16. 2元一次方程计算机求解,在Excel中求解二元一次方程组
  17. 电工电子技术基础----multisim7使用及实验过程
  18. mac pem证书登陆linux,Mac通过SSH使用PEM文件登录服务器
  19. 模电笔记3 三极管 光电三极管
  20. 苹果手机备份数据到电脑什么位置 iPhone备份到电脑上的东西在哪里

热门文章

  1. 微信小程序wx:for 的使用
  2. TMS Scripter importtool的使用
  3. 第三次作业+105032014085
  4. apiCloud中的数据库操作mcm-js-sdk的使用
  5. 如何修改Sql2005注册服务器名称 {转载}
  6. 网页如何与mysql服务器建立连接不上,html与mysql建立连接数据库
  7. python时间序列动图_手把手教你用Python进行时间序列分解和预测
  8. java工具集_Java 工具集
  9. ESI计算机排名2021年01月,喜忧参半!2021年5月ESI世界大学排行榜,21个学科排名数据分析!...
  10. php语句中两个if语句,如何在PHP中组合两个IF语句