Can Place Flowers
来源:互联网 发布:淘宝考试不 编辑:程序博客网 时间:2024/06/05 10:55
题目详情:https://leetcode.com/problems/can-place-flowers/description/
# -*- coding:utf-8 -*-class Solution(object): def canPlaceFlowers(self, flowerbed, n): """ :type flowerbed: List[int] :type n: int :rtype: bool """ if n==0: #n为需要种植的花的个数,个数为0,说明不需要种植 return True i,length=0,len(flowerbed) flowerbed.append(0)#在flowerbed的最后添加0,这样便于操作。主要便于操作最前边和最后边有0的情况 while i<length and n!=0: if flowerbed[i]==1:#如果flowerbed[i]==1,那么下一个位置不管是0还是1,都不符合条件 i=i+2#所以直接检查(i+2)的位置 elif flowerbed[i-1]==0 and flowerbed[i+1]==0:#flowerbed[i]==0并且前后都为0,那么这个位置可以种植 flowerbed[i]=1#将该位置种上花 i,n=i+2,n-1#那么下一个位置也是不符合条件的,直接检查(i+2)位置,并将需要种植的个数减1 else:#其余的情况,检查(i+1)位置 i=i+1 if n==0:#循环结束,检查n是否为0,为0说明可以种植n颗花 return True return False#否则,不能种植n颗花
阅读全文
0 0
- 605. Can Place Flowers
- Can Place Flowers
- 605. Can Place Flowers
- 605Can Place Flowers
- 605. Can Place Flowers
- leetcode605. Can Place Flowers
- 605. Can Place Flowers
- 605. Can Place Flowers
- 605. Can Place Flowers
- Can Place Flowers
- leetcode[Can Place Flowers]
- 605. Can Place Flowers
- Can Place Flowers
- 605. Can Place Flowers
- leetcode-Can Place Flowers
- Can Place Flowers
- 605. Can Place Flowers
- leetCode-Can Place Flowers
- 自定义view group 1
- C# 调用mysql存储过程
- spak源码分析之sparkContext源码分析
- Java8新特性——内建的功能性接口
- 萌新的linux之旅24
- Can Place Flowers
- 为自己的人生负责
- 2017.8.19总结1-经济编码
- java8时间 yyyyMMddHHmmss格式转为日期
- Kotlin入门篇(六),list和map的使用
- cookie,session,token
- 14.进程和信号
- linux
- ListView使用SimpleAdapter适配器