敢教日月换新天。为有牺牲多壮志,

[Swift]LeetCode441. 排列硬币 | Arranging Coins

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10339427.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

You have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins.

Given n, find the total number of fullstaircase rows that can be formed.

n is a non-negative integer and fits within the range of a 32-bit signed integer.

Example 1:

n = 5

The coins can form the following rows:
¤
¤ ¤
¤ ¤

Because the 3rd row is incomplete, we return 2. 

Example 2:

n = 8

The coins can form the following rows:
¤
¤ ¤
¤ ¤ ¤
¤ ¤

Because the 4th row is incomplete, we return 3.

你总共有 枚硬币,你需要将它们摆成一个阶梯形状,第 行就必须正好有 枚硬币。

给定一个数字 n,找出可形成完整阶梯行的总行数。

是一个非负整数,并且在32位有符号整型的范围内。

示例 1:

n = 5

硬币可排列成以下几行:
¤
¤ ¤
¤ ¤

因为第三行不完整,所以返回2.

示例 2:

n = 8

硬币可排列成以下几行:
¤
¤ ¤
¤ ¤ ¤
¤ ¤

因为第四行不完整,所以返回3.

20ms
1 class Solution {
2     func arrangeCoins(_ n: Int) -> Int {
3         return Int((-1 + sqrt(Double(1 + 8 * n))) / 2)
4     }
5 }

28ms

复制代码
 1 class Solution {
 2     func arrangeCoins(_ n: Int) -> Int {
 3             guard n > 1 else {
 4         return n
 5     }
 6     var index = n, lhs = 0, rhs = n, mid = 0
 7     while lhs < rhs {
 8         mid = (rhs + lhs) / 2
 9         let result = (1 + mid) * mid / 2
10         if result == n {
11             return mid
12         } else if result > n {
13             rhs = mid
14         } else {
15             lhs = mid + 1
16         }
17     }
18     return lhs - 1
19     }
20 }
复制代码

36ms

复制代码
 1 class Solution {
 2     func arrangeCoins(_ n: Int) -> Int {
 3         var left = 1
 4         var right = n
 5 
 6         while left <= right {
 7             let mid = left + (right - left) / 2
 8             if n * 2 < mid * (mid + 1) {
 9                 right = mid - 1
10             } else {
11                 left = mid + 1
12             }
13         }
14 
15         return left - 1
16     }
17 }
复制代码

52ms

复制代码
1 class Solution {
2     func arrangeCoins(_ n: Int) -> Int {
3         var result = Int(sqrt(Double(n * 2))) - 1
4         while result * (result + 1) / 2 <= n {
5             result += 1
6         }
7         return result - 1
8     }    
9 }
复制代码

80ms

复制代码
 1 class Solution {
 2     func arrangeCoins(_ n: Int) -> Int {
 3         var row = 1
 4         var sum = row
 5         while true {
 6             if sum == n {
 7                 return row
 8             } else if sum > n {
 9                 return row - 1
10             }
11             row += 1
12             sum += row
13         }
14     }
15 }
复制代码

176ms

复制代码
 1 class Solution {
 2     func arrangeCoins(_ n: Int) -> Int {
 3     return self.lineCoins(n, 1)
 4     }
 5         func lineCoins(_ n: Int, _ line: Int) -> Int {
 6         if n<line {
 7             return line-1
 8         }
 9         return lineCoins(n-line, line+1);
10     }
11 }
复制代码

204ms

复制代码
 1 class Solution {
 2     func arrangeCoins(_ n: Int) -> Int {
 3         if n == 0 || n == 1 {
 4             return n
 5         }
 6         for value in 1...n {
 7             if Double((1 + value) * value) * 0.5 > Double(n) {
 8                 return (value - 1)
 9             }
10         }
11         return 0
12     }
13 }
复制代码

 

posted @   为敢技术  阅读(277)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示
哥伦布
09:09发布
哥伦布
09:09发布
3°
多云
东南风
3级
空气质量
相对湿度
47%
今天
中雨
3°/15°
周三
中雨
3°/13°
周四
小雪
-1°/6°