首页 > 学院 > 开发设计 > 正文

[LeetCode]441. Arranging Coins

2019-11-06 08:08:23
字体:
来源:转载
供稿:网友

[LeetCode]441. Arranging Coins

题目描述

这里写图片描述

思路

第一行堆叠1列 第二行堆叠2列 。。。 第n行堆叠n列 循环运算 对于0,返回0


update 规律,等差数列求和 (n * (n + 1)) / 2 = num n = sqrt(num * 2 + 1/4) - 1/2 对于n向下取整即可

代码

class Solution {public: int arrangeCoins(int n) { int count = 0; while (n >= count) { n -= count; count++; } if (count){ count--; } return count; }};

update

class Solution {public: int arrangeCoins(int n) { double num = 2.0 * n + 0.25; return (int)(sqrt(num) - 0.5); }};
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表