zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    ECharts
    JavaScript实现数组去重方法
    面试题1
    Vue中的8种组件通信方式
    渐进式web应用 (PWA)
    单元测试(Jest 和 Mocha)
    axios
    Vue Router
    Vuex
    hash 模式与 history 模式小记
  • 原文地址:https://www.cnblogs.com/panweiwei/p/12799834.html
Copyright © 2011-2022 走看看