没想到好的办法 只能用笨办法去解了 因为没考虑到一些特殊情况 提交失败两次。
相关代码
class Solution(object):
def canPlaceFlowers(self, flowerbed, n):
default_count = flowerbed.count(1)
if flowerbed[0:2] == [0, 0]:
flowerbed[0] = 1
if flowerbed[-2:] == [0, 0]:
flowerbed[-1] = 1
if flowerbed == [0]:
flowerbed = [1]
flowerbed_str = ''.join([str(x) for x in flowerbed])
while '000' in flowerbed_str:
flowerbed_str = flowerbed_str.replace('000','010')
return flowerbed_str.replace('000','010').count('1') - default_count >= n
网友评论