441. Arranging Coins
来源:互联网 发布:三浦翔平人不好知乎 编辑:程序博客网 时间:2024/05/22 12:45
题目:
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 = 5The coins can form the following rows:¤¤ ¤¤ ¤Because the 3rd row is incomplete, we return 2.
Example 2:
n = 8The coins can form the following rows:¤¤ ¤¤ ¤ ¤¤ ¤Because the 4th row is incomplete, we return 3.思路:
本题塔型数据求和问题,思路比较简单,单独是先一个求和函数,利用递归实现,为了防止数据溢出
代码:
class Solution {public: int arrangeCoins(int n) { long num = n; for(int i = sqrt(2*num);i>=0;i--) { if(sum(i)<=num) return i; } return 0; }private: int sum(int n){ if(n==0) return 0; return sum(n-1)+n; }};
阅读全文
0 0
- LeetCode 441. Arranging Coins
- 441. Arranging Coins
- 441. Arranging Coins
- 441. Arranging Coins
- 441. Arranging Coins
- LeetCode 441. Arranging Coins
- 441. Arranging Coins
- 441. Arranging Coins
- leetcode 441. Arranging Coins
- 441. Arranging Coins
- Leetcode: 441. Arranging Coins
- LeetCode 441. Arranging Coins
- 441. Arranging Coins
- 【LeetCode】 441. Arranging Coins
- LeetCode 441. Arranging Coins
- [leetcode]441. Arranging Coins
- 441. Arranging Coins
- 441. Arranging Coins*
- Filter过滤器【笔记】
- 期权激励的十大问题
- mysql union用解详细
- 2017 Multi-University Training Contest
- 布尔模型(Boolean Model)
- 441. Arranging Coins
- C/C++/PHP等语言 在线编译器
- hdu3047 (带权并查集)
- ICommand简单的使用和开发
- eBay数据科学家李睿:自然语言处理在eBay的技术实践 数据 网络 类别 技术 分类器 阅读1593 近日,在飞马网主办的“FMI人工智能&大数据高峰论坛”上,来自eBay的数据科学家李睿
- 浅谈javaw.exe的作用
- 【勤哲应用】用勤哲Excel服务器做人事工资管理系统
- NYISTME 1300 递推求值
- poj-2773(数论+容斥原理+二分)