你总共有 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 排列硬币的更多相关文章

  1. [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 ...

  2. [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 ...

  3. 【leetcode】441. Arranging Coins

    problem 441. Arranging Coins solution1: class Solution { public: int arrangeCoins(int n) { ; ; while ...

  4. 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 ...

  5. Leetcode441Arranging Coins排列硬币

    你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状,第 k 行就必须正好有 k 枚硬币. 给定一个数字 n,找出可形成完整阶梯行的总行数. n 是一个非负整数,并且在32位有符号整型的范围内. 示例 ...

  6. 【LeetCode】441. Arranging Coins 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 模拟计算 二分查找 数学公式 日期 题目地址:htt ...

  7. 441. Arranging Coins

    static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...

  8. 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 ...

  9. Leetcode之二分法专题-441. 排列硬币(Arranging Coins)

    Leetcode之二分法专题-441. 排列硬币(Arranging Coins) 你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状,第 k 行就必须正好有 k 枚硬币. 给定一个数字 n,找出可形 ...

随机推荐

  1. poj1161Post Office【经典dp】

    题目:poj1161Post Officeid=1160" target="_blank">点击打开链接 题意:给出一条直线上的n个坐标表示村庄的位置,然后要在上面 ...

  2. VS code - code Snippet

    For anyone working on the UI and using VS Code, you can create a user Snippet and keyboard shortcut ...

  3. 使用JS对select标签进行联动选择

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  4. MaterialImageView

    https://github.com/zhaozhentao/MaterialImageView

  5. HTML5开发实战——Sencha Touch篇(1)

    学习了很多主要的Sencha Touch内容,已经了解了Sencha Touch的开发模式.接下来一段时间我们将利用Sencha Touch来进行自己的web应用构建. 先要解决的是前端的问题.从最简 ...

  6. DLR之 ExpandoObject和DynamicObject的使用演示样例

    ExpandoObject :动态的增删一个对象的属性,在低层库(比如ORM)中非常实用.因为ExpandoObject实现了IDictionay<string, object>接口,常见 ...

  7. NHibernate不支持复杂的linq,就一定要用DataTable这么低级吗

    有些linq,好不容易写出来,正想扬眉吐屁一番,不料用NHibernate一执行,却报错,说是不支持,我靠. 只好捏着鼻子写一大段sql,交给它.这种直接执行SQL的情况,我看我同事写的,全部都是返回 ...

  8. BZOJ1016 &amp;&amp; JSOI2008] 最小生成树计数

    题目链接:id=1016">点击打开链接 裸题 #pragma comment(linker, "/STACK:1024000000,1024000000") #i ...

  9. URAL1099 Work Scheduling —— 一般图匹配带花树

    题目链接:https://vjudge.net/problem/URAL-1099 1099. Work Scheduling Time limit: 0.5 secondMemory limit: ...

  10. codeforces 688A A. Opponents(水题)

    题目链接: A. Opponents time limit per test 1 second memory limit per test 256 megabytes input standard i ...