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. 新手使用R的注意事项

    1.最好先设置工作目录 如: setwd(“D:/DataDig”) 注意不是”\”,是”/” 再读取数据,如: datafile = read.csv("./test.csv") ...

  2. CentOS的SSH,Putty配置说明

    基本资源: CentOS5.5 (32位) , Mysql6.0 ,Putty ,SSH   Step: 1.VMWare中装好CentOS    A. 可能存在ifconfig等命令无法正常识别) ...

  3. dfs序 + RMQ = LCA

    dfs序是指你用dfs遍历一棵树时,每个节点会按照遍历到的先后顺序得到一个序号.然后你用这些序号,可以把整个遍历过程表示出来. 如上图所示,则整个遍历过程为1 2 3 2 4 5 4 6 4 2 1 ...

  4. 【C语言入门教程】4.8 指针数组

    指针数组是一种特殊的数组,这类数组存放的全部是同一数据类型的内存地址.指针数组的定义形式为: 数据类型 *数组名[长度]; 例如: const char *c[4] = { "China&q ...

  5. CK-Editor content.replace

    <s:property value="content.replace('\n\r', '<br/>')" escape="false"/> ...

  6. C#网络爬虫 WebUtility使用 转义字符 urlCode

    背景: 在C#写网络爬虫时候,有时候需要将html中的转义字符进行处理,还有网址中的中文处理 一.html转义字符处理 1.ASP.NET中的html解析 HttpUtility.HtmlDecode ...

  7. Redhat EL安装curses

    1.下载curses安装包 http://ftp.gnu.org/pub/gnu/ncurses/ncurses-5.6.tar.gz2. tar -zxvf  nurses-5.6.tar.gz 3 ...

  8. gcc 和g++区别

    gcc和g++都是GNU的一个编译器;这两者的区别:1.从源文件上看,对于文件后缀(扩展名)为.c的test.c文件,gcc会把它看成是C程序,而g++则会把它看成是C++程序;而对于文件后缀(扩展名 ...

  9. 【转】phpcms基础内容

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

  10. JavaScript中的跨域

    跨域是什么 跨域就是指从一个域名的网页去请求另一个域名的资源,因为JavaScript同源策略的限制,资源无法获取.比如从www.baidu.com 页面去请求 www.google.com 的资源, ...