博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Swift]LeetCode605. 种花问题 | Can Place Flowers
阅读量:4950 次
发布时间:2019-06-11

本文共 6575 字,大约阅读时间需要 21 分钟。

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

➤微信公众号:山青咏芝(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:

  1. The input array won't violate no-adjacent-flowers rule.
  2. The input array size is in the range of [1, 20000].
  3. 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. 数组内已种好的花不会违反种植规则。
  2. 输入的数组长度范围为 [1, 20000]。
  3. n 是非负整数,且不会超过输入数组的大小。

Runtime: 128 ms
Memory Usage: 19.3 MB
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 }

 

转载于:https://www.cnblogs.com/strengthen/p/10455207.html

你可能感兴趣的文章
[JavaEE]Hibernate 所有缓存机制详解
查看>>
python-面向对象(股票对象举例)
查看>>
AnkhSVN 简体中文版 2.3.11249.4534
查看>>
chrome收藏夹整理
查看>>
用ctrl+鼠标滚动调节字体大小
查看>>
查数据库有哪些表、查数据库
查看>>
使用git上传github遇到的问题
查看>>
静态页面菜单栏布局整改使用iframeset
查看>>
图解HTTP学习笔记——简单的HTTP协议
查看>>
Java Finally
查看>>
卑鄙的外乡人——公共知识与共有知识
查看>>
把只包含质因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含质因子7。 习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。...
查看>>
Spring中的Advisor,Advice,Pointcut
查看>>
20165223 实验二 面向对象程序设计
查看>>
30岁前不要让人生留下遗憾笔记
查看>>
如何注册EPIMATE
查看>>
交易进行中买家申请退货退款操作流程
查看>>
常用技巧之JS判断数组中某元素出现次数
查看>>
Oracle命令:授权-收回权限-角色-用户状态
查看>>
云打码识别验证码
查看>>