其他分享
首页 > 其他分享> > 605. 种花问题

605. 种花问题

作者:互联网

 

 

 

 

 

 

代码一:

 1 class Solution(object):
 2     def canPlaceFlowers(self, flowerbed, n):
 3         """
 4         :type flowerbed: List[int]
 5         :type n: int
 6         :rtype: bool
 7         """
 8         if len(flowerbed) == 1 and flowerbed[0] == 0:
 9             flowerbed[0] = 1
10             n -= 1
11             return n <= 0
12         if flowerbed[0] == 0 and flowerbed[1] == 0:
13             flowerbed[0] = 1
14             n -= 1
15         for i in range(1, len(flowerbed) - 1):
16             if n <= 0:
17                 return True
18             if flowerbed[i - 1] == 0 and flowerbed[i] == 0 and flowerbed[i + 1] == 0:
19                 flowerbed[i] = 1
20                 n -= 1
21         if flowerbed[-1] == 0 and flowerbed[-2] == 0:
22             flowerbed[-1] = 1
23             n -= 1
24         return n <= 0

代码二:

 1 class Solution(object):
 2     def canPlaceFlowers(self, flowerbed, n):
 3         """
 4         :type flowerbed: List[int]
 5         :type n: int
 6         :rtype: bool
 7         """
 8         flowerbed = [0] + flowerbed + [0]
 9         for i in range(1, len(flowerbed)-1):
10             if n <= 0:
11                 return True
12             if flowerbed[i - 1] == 0 and flowerbed[i] == 0 and flowerbed[i + 1] == 0:
13                 flowerbed[i] = 1
14                 n -= 1
15         return n <= 0

 

标签:605,10,canPlaceFlowers,rtype,int,flowerbed,问题,种花,type
来源: https://www.cnblogs.com/panweiwei/p/12799834.html