441 Arranging Coins 排列硬币
你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状,第 k 行就必须正好有 k 枚硬币。
给定一个数字 n,找出可形成完整阶梯行的总行数。
n 是一个非负整数,并且在32位有符号整型的范围内。
示例 1:
n = 5
硬币可排列成以下几行:
¤
¤ ¤
¤ ¤
因为第三行不完整,所以返回2.
示例 2:
n = 8
硬币可排列成以下几行:
¤
¤ ¤
¤ ¤ ¤
¤ ¤
因为第四行不完整,所以返回3.
详见:https://leetcode.com/problems/arranging-coins/description/
C++:
方法一:
class Solution {
public:
int arrangeCoins(int n) {
int cur=1,retain=n-1;
while(cur+1<=retain)
{
++cur;
retain-=cur;
}
return n==0?0:cur;
}
};
方法二:
class Solution {
public:
int arrangeCoins(int n) {
if (n <= 1)
{
return n;
}
long low = 1, high = n;
while (low < high)
{
long mid = low + (high - low) / 2;
if (mid * (mid + 1) / 2 <= n)
{
low = mid + 1;
}
else
{
high = mid;
}
}
return low - 1;
}
};
参考:https://www.cnblogs.com/grandyang/p/6026066.html
441 Arranging Coins 排列硬币的更多相关文章
- [LeetCode] 441. Arranging Coins 排列硬币
You have a total of n coins that you want to form in a staircase shape, where every k-th row must ha ...
- [LeetCode] Arranging Coins 排列硬币
You have a total of n coins that you want to form in a staircase shape, where every k-th row must ha ...
- 【leetcode】441. Arranging Coins
problem 441. Arranging Coins solution1: class Solution { public: int arrangeCoins(int n) { ; ; while ...
- LeetCode 441 Arranging Coins
Problem: You have a total of n coins that you want to form in a staircase shape, where every k-th ro ...
- Leetcode441Arranging Coins排列硬币
你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状,第 k 行就必须正好有 k 枚硬币. 给定一个数字 n,找出可形成完整阶梯行的总行数. n 是一个非负整数,并且在32位有符号整型的范围内. 示例 ...
- 【LeetCode】441. Arranging Coins 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 模拟计算 二分查找 数学公式 日期 题目地址:htt ...
- 441. Arranging Coins
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
- LeetCode_441. Arranging Coins
441. Arranging Coins Easy You have a total of n coins that you want to form in a staircase shape, wh ...
- Leetcode之二分法专题-441. 排列硬币(Arranging Coins)
Leetcode之二分法专题-441. 排列硬币(Arranging Coins) 你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状,第 k 行就必须正好有 k 枚硬币. 给定一个数字 n,找出可形 ...
随机推荐
- glTF格式初步了解
glTF格式初步了解 近期看到Qt 3D的进展.偶然了解到了一种新的格式:glTF格式.这样的格式据说比现有的3D格式更加符合OpenGL应用的须要.这引起了我的好奇.于是我在Qt 3D的外部链接中找 ...
- easyUI 验证控件应用、自己定义、扩展验证 手机号码或电话话码格式
easyUI 验证控件应用.自己定义.扩展验证 手机号码或电话话码格式 在API中 发现给的demo 中没有这个验证,所以就研究了下. 相关介绍省略,直接上代码吧! watermark/2/tex ...
- Android KK后为何工厂模式下无法adb 无法重新启动机器 ?
前言 欢迎大家我分享和推荐好用的代码段~~ 声明 欢迎转载,但请保留文章原始出处: CSDN:http://www.csdn.net ...
- 【转】TestNG执行顺序控制
1.class执行顺序控制---testng.xml之preserve-order preserve-order:用来控制<test>里面所有<classes>的执行顺序.&l ...
- 使用PowerShell 创建SharePoint 站点
使用PowerShell 创建SharePoint 站点 在SharePoint开发中,你应该学会使用PowerShell管理SharePoint网站.SharePoint Manag ...
- lsblk df
df(1) - Linux manual page http://man7.org/linux/man-pages/man1/df.1.html report file system disk spa ...
- LeetCode 7. Reverse Integer (JS)
Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...
- mongodb 关闭服务 mongod -f /root/mongodb/bin/xx.conf --shutdown
/root/mongodb/bin/mongod -f /root/mongodb/bin/xx.conf --shutdown
- 并不对劲的bzoj3998:loj2102:p3975:[TJOI2015]弦论
题目大意 对于一个给定的长度为n(\(n\leq5*10^5\))的字符串, 分别求出不同位置的相同子串算作一个.不同位置的相同子串算作多个时,它的第k(\(k\leq10^9\))小子串是什么 题解 ...
- BZOJ_1067_[SCOI2007]降雨量_ST表
BZOJ_1067_[SCOI2007]降雨量_ST表 Description 我们常常会说这样的话:“X年是自Y年以来降雨量最多的”.它的含义是X年的降雨量不超过Y年,且对于任意 Y<Z< ...