Problem:

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 full staircase 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.

Summary:

用n枚硬币摆成塔形,求可以摆成的完整的行数。

Analysis:

1.最简单的思路,依次减去递增的每行硬币数,直到n为非整数。

 class Solution {
public:
int arrangeCoins(int n) {
int i = ;
while (n > ) {
i++;
n -= i;
} return n == ? i : i - ;
}
};

2. 解一元二次方程:x^2 + x = 2 * n 解得:x = sqrt(2 * n + 1 / 4) - 1 /2

但要注意在此处n为32位有符号整型数,2 * n后有可能溢出,故在代码中应做相应处理。

 class Solution {
public:
int arrangeCoins(int n) {
return sqrt((long long) * n + 0.25) - 0.5;
}
};

LeetCode 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】441. Arranging Coins

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

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

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

  4. [LeetCode] 441. Arranging Coins_Easy tag: Math

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

  5. 441 Arranging Coins 排列硬币

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

  6. 441. Arranging Coins

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

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

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

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

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

随机推荐

  1. sql拼音简写函数

    USE [HotelDB]GO /****** Object: UserDefinedFunction [dbo].[fn_GetPy] Script Date: 2016/1/4 13:29:13 ...

  2. WPF中的数据绑定!!!

    引用自:https://msdn.microsoft.com/zh-cn/magazine/cc163299.aspx  数据点: WPF 中的数据绑定 数据点 WPF 中的数据绑定 John Pap ...

  3. 【AngularJS】—— 1 初识AngularJs

    怀着激动与忐忑的心情,开始了学习AngularJS的旅程,很久之前就听说了这个前端框架,但是由于自己一直没有从事相关的工作,因此也没有进行学习.这次正好学习AngularJS,直接复习一下前端的知识. ...

  4. hadoop之 flume1.6安装

    flume 1.6安装1.解压 2.复制 cp conf/flume-conf.properties.template conf/flume.conf cp conf/flume-env.sh.tem ...

  5. Ubuntu 12 安装 MySQL 5.6.26 及 问题汇总

    参考先前的文章:Ubuntu 14 编译安装 PHP 5.4.45 + Nginx 1.4.7 + MySQL 5.6.26 笔记 安装过程: #安装依赖库 sudo apt-get install ...

  6. plink远程连接服务器进行编译

    脚本命令: echo y|D:\remote_link\plink  -l  user  -pw password  172.16.0.101 "export LANG=en_US;cd / ...

  7. 【转】phpcms基础内容

    <?php 思路: 一.目前在企业中使用比较多的cms内容管理有如下几种: 1.dedecms 2.phpcms 二.我们选择学习v9版本的phpcms,主要有以下几点原因: 1.基于MVC模式 ...

  8. [POJ2586]Y2K Accounting Bug

    [POJ2586]Y2K Accounting Bug 试题描述 Accounting for Computer Machinists (ACM) has sufferred from the Y2K ...

  9. 安装ssh服务

    1.先更新下源 sudo apt-get update 2.安装ssh服务 sudo apt-get openssh-server 3.配置ssh-server,配置文件位于/etc/ssh/sshd ...

  10. Android使用Unity导致Activity被销毁的解决办法

    由于需要在Android中使用Unity(Android的Activity会继承Unity提供的UnityPlayerActivity),可能是第三方的原因退出Unity后就导致Android整个应用 ...