★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝()➤GitHub地址:➤原文地址: ➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, flowers cannot be planted in adjacent plots - they would compete for water and both would die.
Given a flowerbed (represented as an array containing 0 and 1, where 0 means empty and 1 means not empty), and a number n, return if n new flowers can be planted in it without violating the no-adjacent-flowers rule.
Example 1:
Input: flowerbed = [1,0,0,0,1], n = 1Output: True
Example 2:
Input: flowerbed = [1,0,0,0,1], n = 2Output: False
Note:
- The input array won't violate no-adjacent-flowers rule.
- The input array size is in the range of [1, 20000].
- n is a non-negative integer which won't exceed the input array size.
假设你有一个很长的花坛,一部分地块种植了花,另一部分却没有。可是,花卉不能种植在相邻的地块上,它们会争夺水源,两者都会死去。
给定一个花坛(表示为一个数组包含0和1,其中0表示没种植花,1表示种植了花),和一个数 n 。能否在不打破种植规则的情况下种入 n 朵花?能则返回True,不能则返回False。
示例 1:
输入: flowerbed = [1,0,0,0,1], n = 1输出: True
示例 2:
输入: flowerbed = [1,0,0,0,1], n = 2输出: False
注意:
- 数组内已种好的花不会违反种植规则。
- 输入的数组长度范围为 [1, 20000]。
- n 是非负整数,且不会超过输入数组的大小。
1 class Solution { 2 func canPlaceFlowers(_ flowerbed: [Int], _ n: Int) -> Bool { 3 var flowerbed = flowerbed 4 var n = n 5 flowerbed.insert(0,at:0) 6 flowerbed.append(0) 7 var i:Int = 1 8 while(i < flowerbed.count - 1) 9 {10 if n == 0 { return true}11 if flowerbed[i - 1] + flowerbed[i] + flowerbed[i + 1] == 012 {13 n -= 114 i += 1 15 }16 i += 1 17 }18 return n <= 019 }20 }
132ms
1 class Solution { 2 func canPlaceFlowers(_ flowerbed: [Int], _ n: Int) -> Bool { 3 if n == 0{ 4 return true 5 } 6 var pending = n 7 8 var currentIndex = 0 9 var foundOne = false10 while currentIndex < flowerbed.count{11 if flowerbed[currentIndex] == 0{12 if foundOne || (currentIndex < flowerbed.count - 1 && flowerbed[currentIndex + 1] == 1){13 foundOne = false14 currentIndex += 115 }16 else{17 pending -= 118 if pending == 0{19 return true20 }21 currentIndex += 222 }23 }24 else if flowerbed[currentIndex] == 1{25 foundOne = true26 currentIndex += 127 }28 else{29 currentIndex += 130 }31 }32 return pending == 033 }34 }
136ms
1 class Solution { 2 func canPlaceFlowers(_ flowerbed: [Int], _ n: Int) -> Bool { 3 guard n > 0 else { 4 return true 5 } 6 7 var left = n 8 var newArray = [0] 9 newArray.append(contentsOf: flowerbed)10 newArray.append(0)11 var i = 012 while i < newArray.count - 2 {13 if newArray[i] == 0 && newArray[i+1] == 0 && newArray[i+2] == 0 {14 left -= 115 i += 116 if left == 0 {17 return true18 }19 }20 i += 121 }22 return left == 023 }24 }
140ms
1 class Solution { 2 func canPlaceFlowers(_ flowerbed: [Int], _ n: Int) -> Bool { 3 var flowerbed = flowerbed 4 var n = n 5 var i = 0 6 while i < flowerbed.count && n > 0 { 7 if flowerbed[i] == 0 && (i == 0 || flowerbed[i - 1] == 0) && (i == flowerbed.count - 1 || flowerbed[i + 1] == 0) { 8 n -= 1 9 flowerbed[i] = 110 }11 i += 112 }13 return n == 014 }15 }
144ms
1 class Solution { 2 func canPlaceFlowers(_ flowerbed: [Int], _ n: Int) -> Bool { 3 var result = 0 4 var count = 1 5 for b in flowerbed { 6 if b == 0 { 7 count += 1 8 } else if b == 1 { 9 result += (count-1)/210 count = 011 }12 }13 result += count / 214 return result >= n15 }16 }
152ms
1 class Solution { 2 func canPlaceFlowers(_ flowerbed: [Int], _ n: Int) -> Bool { 3 if n == 0 { return true} 4 5 6 var pre = -2 7 var count = n 8 for (index, num) in flowerbed.enumerated() { 9 if num == 1 {10 count -= (index - pre - 1 - 1) / 211 pre = index12 }13 }14 count -= (flowerbed.count - pre - 1) / 215 return count <= 016 }17 }
172ms
1 class Solution { 2 func canPlaceFlowers(_ flowerbed: [Int], _ n: Int) -> Bool { 3 // how many possible position 4 var adjcent = 0 5 // how many consequent 0 start with 1 because plant can be placed at first position it means the left of first position. 6 var c = 1 7 for i in 0..= n18 }19 }
180ms
1 class Solution { 2 func canPlaceFlowers(_ flowerbed: [Int], _ n: Int) -> Bool { 3 var flowerPlaced = 0 4 5 var lastOne = -1 6 var i = 0 7 while i < flowerbed.count { 8 while i < flowerbed.count && flowerbed[i] == 0 { 9 i += 110 }11 12 let space = i - lastOne - 113 14 if lastOne >= 0 && i < flowerbed.count {15 flowerPlaced += (space - 1) / 216 } else if lastOne == -1 && i == flowerbed.count {17 flowerPlaced += (space + 1) / 218 } else {19 flowerPlaced += space / 220 }21 22 if flowerPlaced >= n {23 return true24 }25 26 while i < flowerbed.count && flowerbed[i] == 1 {27 i += 128 }29 lastOne = i - 130 }31 32 return false33 }34 }
196ms
1 class Solution { 2 func canPlaceFlowers(_ flowerbed: [Int], _ n: Int) -> Bool { 3 var planted = 0 4 5 var space = 1 6 7 for plot in flowerbed { 8 if plot == 0 { 9 space += 110 } else {11 planted += (space - 1) / 212 space = 013 }14 }15 16 return (planted + space / 2) >= n17 }18 }