go lang solution


  • 0
    A
    func canPlaceFlowers(flowerbed []int, n int) bool {
        
        l := len(flowerbed)
        if l == 1 && flowerbed[0] == 0{
                flowerbed[0] = 1
                n--
        }
        
        if flowerbed[0] == 0 &&  flowerbed[1]  == 0 {
            flowerbed[0] = 1
            n--
        }
        
        for i:=1;i<l;i++{
            if flowerbed[i] == 0 && flowerbed[i-1] == 0{
                if i == l - 1 || flowerbed[i+1] == 0 {
                    flowerbed[i] = 1
                    n--
                }
            }
        }
        return n <= 0
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.