LeetCode--441--排列硬币
问题描述:
你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状,第 k 行就必须正好有 k 枚硬币。
给定一个数字 n,找出可形成完整阶梯行的总行数。
n 是一个非负整数,并且在32位有符号整型的范围内。
示例 1:
n = 5 硬币可排列成以下几行:
¤
¤ ¤
¤ ¤ 因为第三行不完整,所以返回2.
示例 2:
n = 8 硬币可排列成以下几行:
¤
¤ ¤
¤ ¤ ¤
¤ ¤ 因为第四行不完整,所以返回3.
方法1:
class Solution(object):
def arrangeCoins(self, n):
"""
:type n: int
:rtype: int
"""
low = 0
high = n while low<=high:
mid = int((low+high)//2) if mid*(mid+1)/2 <= n < (mid+1)*(mid+2)/2:
return mid
elif mid*(mid+1)/2 > n:
high = mid-1
elif (mid+1)*(mid+2)/2 <= n:
low = mid + 1
AMAZING:
class Solution(object):
def arrangeCoins(self, n):
return int(((8*n + 1)**0.5 - 1)/2)
同上:
class Solution(object):
def arrangeCoins(self, n):
"""
:type n: int
:rtype: int
"""
k = int((2*n+0.25)**0.5-0.5)
return k
2018-10-03 21:33:05
LeetCode--441--排列硬币的更多相关文章
- Java实现 LeetCode 441 排列硬币
441. 排列硬币 你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状,第 k 行就必须正好有 k 枚硬币. 给定一个数字 n,找出可形成完整阶梯行的总行数. n 是一个非负整数,并且在32位有符号整 ...
- LeetCode 441.排列硬币(C++)
你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状,第 k 行就必须正好有 k 枚硬币. 给定一个数字 n,找出可形成完整阶梯行的总行数. n 是一个非负整数,并且在32位有符号整型的范围内. 示例 ...
- leetcode 441.排列硬币(python)
1.题目描述 你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状,第 k 行就必须正好有 k 枚硬币. 给定一个数字 n,找出可形成完整阶梯行的总行数. n 是一个非负整数,并且在32位有符号整型的范 ...
- Leetcode之二分法专题-441. 排列硬币(Arranging Coins)
Leetcode之二分法专题-441. 排列硬币(Arranging Coins) 你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状,第 k 行就必须正好有 k 枚硬币. 给定一个数字 n,找出可形 ...
- 【LeetCode】排列硬币
[问题]你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状,第 k 行就必须正好有 k 枚硬币.给定一个数字 n,找出可形成完整阶梯行的总行数.n 是一个非负整数,并且在32位有符号整型的范围内. [ ...
- [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] 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 ...
- 441 Arranging Coins 排列硬币
你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状,第 k 行就必须正好有 k 枚硬币.给定一个数字 n,找出可形成完整阶梯行的总行数.n 是一个非负整数,并且在32位有符号整型的范围内.示例 1:n ...
- [Swift]LeetCode441. 排列硬币 | 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] Coin Change 硬币找零
You are given coins of different denominations and a total amount of money amount. Write a function ...
随机推荐
- Bootstrap3基础 btn-group-vertical 按钮组(横着、竖着排列)
内容 参数 OS Windows 10 x64 browser Firefox 65.0.2 framework Bootstrap 3.3.7 editor ...
- 高通平台framework,hal,kernel打开log【转】
本文转载自:https://blog.csdn.net/u010164190/article/details/78625636 .Add framework log #define LOG_NDEBU ...
- Cmder + Babun 打造 Windows 好用的终端工具
Babun a windows shell you will love Babun features the following: Pre-configured Cygwin with a lot o ...
- jquery里面获取div区块的宽度与高度
https://blog.csdn.net/ll641058431/article/details/52768825 获取宽度 $('div').width(); 获取:区块的本身宽度 $(' ...
- WinMerge 过滤器用法
WinMerge是一款开源的文件对比合并工具.http://winmerge.org/WinMerge提供了“过滤器”功能,可以在对比时排除特定的目录或文件. 1.编辑过滤规则工具 -> 过滤器 ...
- P5057 [CQOI2006]简单题(线段树)
果然简单题,5分钟紫题++ 代码 #include <cstdio> #include <algorithm> #include <cstring> using n ...
- 论文笔记之:Deep Attributes Driven Multi-Camera Person Re-identification
Deep Attributes Driven Multi-Camera Person Re-identification 2017-06-28 21:38:55 [Motivation] 本文 ...
- Dependency Injection2
IoC容器和Dependency Injection 模式 使用 Service Locator 依赖注入的最大好处在于:它消除了MovieLister类对具体 MovieFinder实现类的依赖 ...
- js操作css变量
原文:http://css-live.ru/articles/dostup-k-css-peremennym-i-ix-izmenenie-spomoshhyu-javascript.html :ro ...
- 2nd,Python基础2——02
1 列表.元组操作 列表可以对数据实现最方便的存储.修改等操作 names = ['Jack', 'Leon','Eric'] 通过下表访问列表中的元素,下标从0开始计数 names = ['Jack ...