//441:排列硬币
This commit is contained in:
parent
37d963ce4f
commit
8df9e1abb1
59
src/main/java/leetcode/editor/cn/ArrangingCoins.java
Normal file
59
src/main/java/leetcode/editor/cn/ArrangingCoins.java
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
//你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状,第 k 行就必须正好有 k 枚硬币。
|
||||||
|
//
|
||||||
|
// 给定一个数字 n,找出可形成完整阶梯行的总行数。
|
||||||
|
//
|
||||||
|
// n 是一个非负整数,并且在32位有符号整型的范围内。
|
||||||
|
//
|
||||||
|
// 示例 1:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//n = 5
|
||||||
|
//
|
||||||
|
//硬币可排列成以下几行:
|
||||||
|
//¤
|
||||||
|
//¤ ¤
|
||||||
|
//¤ ¤
|
||||||
|
//
|
||||||
|
//因为第三行不完整,所以返回2.
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 2:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//n = 8
|
||||||
|
//
|
||||||
|
//硬币可排列成以下几行:
|
||||||
|
//¤
|
||||||
|
//¤ ¤
|
||||||
|
//¤ ¤ ¤
|
||||||
|
//¤ ¤
|
||||||
|
//
|
||||||
|
//因为第四行不完整,所以返回3.
|
||||||
|
//
|
||||||
|
// Related Topics 数学 二分查找
|
||||||
|
// 👍 113 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
//441:排列硬币
|
||||||
|
class ArrangingCoins {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new ArrangingCoins().new Solution();
|
||||||
|
}
|
||||||
|
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
public int arrangeCoins(long n) {
|
||||||
|
if(n==0){
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
double number = (Math.sqrt(1+8*n)-1)/2;
|
||||||
|
|
||||||
|
return (int)(Math.floor(number));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
33
src/main/java/leetcode/editor/cn/ArrangingCoins.md
Normal file
33
src/main/java/leetcode/editor/cn/ArrangingCoins.md
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
<p>你总共有 <em>n </em>枚硬币,你需要将它们摆成一个阶梯形状,第 <em>k </em>行就必须正好有 <em>k </em>枚硬币。</p>
|
||||||
|
|
||||||
|
<p>给定一个数字 <em>n</em>,找出可形成完整阶梯行的总行数。</p>
|
||||||
|
|
||||||
|
<p><em>n </em>是一个非负整数,并且在32位有符号整型的范围内。</p>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
n = 5
|
||||||
|
|
||||||
|
硬币可排列成以下几行:
|
||||||
|
¤
|
||||||
|
¤ ¤
|
||||||
|
¤ ¤
|
||||||
|
|
||||||
|
因为第三行不完整,所以返回2.
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
n = 8
|
||||||
|
|
||||||
|
硬币可排列成以下几行:
|
||||||
|
¤
|
||||||
|
¤ ¤
|
||||||
|
¤ ¤ ¤
|
||||||
|
¤ ¤
|
||||||
|
|
||||||
|
因为第四行不完整,所以返回3.
|
||||||
|
</pre>
|
||||||
|
<div><div>Related Topics</div><div><li>数学</li><li>二分查找</li></div></div>\n<div><li>👍 113</li><li>👎 0</li></div>
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user