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 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的更多相关文章
- [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】441. Arranging Coins
problem 441. Arranging Coins solution1: class Solution { public: int arrangeCoins(int n) { ; ; while ...
- 【LeetCode】441. Arranging Coins 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 模拟计算 二分查找 数学公式 日期 题目地址:htt ...
- [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 ...
- 441 Arranging Coins 排列硬币
你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状,第 k 行就必须正好有 k 枚硬币.给定一个数字 n,找出可形成完整阶梯行的总行数.n 是一个非负整数,并且在32位有符号整型的范围内.示例 1:n ...
- 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,找出可形 ...
- [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 ...
随机推荐
- taglib
thinkphp中 taglib标签应用 原文出处:http://blog.csdn.net/a11085013/article/details/38172653 1.配置文件中加上: 'APP_AU ...
- protect和private 的区别
protect和private 的区别 public 表示全局,类内部外部子类都可以访问: private表示私有的,只有本类内部可以使用: protected表示受保护的,只有本类或子类或父类中可以 ...
- Cotex-M3内核STM32F10XX系列时钟及其配置方法
一.背景 最近做个项目,需要使用STM32,还是以前一样的观点,时钟就是MCU心脏,供血即时钟频率输出,想要弄明白一个MCU,时钟是一个非常好的切入点.言归正传,网上已经有太多大神详述过STM32的详 ...
- Hadoop之Storm其他_pom
1.pom 配置 <dependency> <groupId>org.apache.storm</groupId> <artifactId>storm- ...
- 【PHP面向对象(OOP)编程入门教程】9.封装性(var与public,protected,private的关系)
封装性是面象对象编程中的三大特性之一,封装性就是把对象的属性和服务结合成一个独立的相同单位,并尽可能隐蔽对象的内部细节,包含两个含义: 1. 把对象的全部属性和全部服务结合在一起,形成一个不可分割的独 ...
- hexo问题篇(偶尔抽抽疯)
hexo安安稳稳的跑了很久,然后 ....让人心碎的hexo问题,华丽丽的摔倒在坑里,只因update了hexo version最是哪一句 hexo server让人欲哭无泪 -问题场景 设备: Ma ...
- UNITY3D在线更新之道-CSlight 使用总结
转自:http://blog.csdn.net/leonwei/article/details/39233775 最近做U3D的热更新,研究了各种方式无果后,最容易最先想到的方式就是利用c#的反射机制 ...
- 如何建立一个完整的游戏AI
http://blog.friskit.me/2012/04/how-to-build-a-perfect-game-ai/ 人工智能(Artificial Intelligence)在游戏中使用已经 ...
- web服务器页面错误代码集
HTTP 1xx-信息提示 这些状态代码表示临时的响应.客户端在收到常规响应之前,应准备接收一个或多个1xx响应. 100-继续. 101-切换协议. 2xx-成功 这类状态代码表明服务器成功地接受了 ...
- Ubuntu+Apache+PHP+Mysql环境搭建
一.操作系统Ubuntu 14.04 64位,虚拟机服务器 二.Apache 1.安装Apache,安装命令:sudo apt-get install apache2 2.环境配置: 1)配置文件:路 ...