Java实现 LeetCode 441 排列硬币
441. 排列硬币
你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状,第 k 行就必须正好有 k 枚硬币。
给定一个数字 n,找出可形成完整阶梯行的总行数。
n 是一个非负整数,并且在32位有符号整型的范围内。
示例 1:
n = 5
硬币可排列成以下几行:
¤
¤ ¤
¤ ¤
因为第三行不完整,所以返回2.
示例 2:
n = 8
硬币可排列成以下几行:
¤
¤ ¤
¤ ¤ ¤
¤ ¤
因为第四行不完整,所以返回3.
PS:
根据数学公式,k(k+1) /2 = n,可以得到其正数解为:k = sqrt(2n+1/4) - 1/2。然后求整即可。
唯一的问题是,这里2n+1/4有可能会超出sqrt函数的参数范围。
于是,我们可以变换一下, k = sqrt(2) * sqrt(n+1/8) - 1/2,这样求平方根就不会超限了。
于是,我们就有了这么一行代码
class Solution {
public int arrangeCoins(int n) {
return (int) (-1 + Math.sqrt(1 + 8 * (long) n)) / 2;
}
}
Java实现 LeetCode 441 排列硬币的更多相关文章
- 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] Coin Change 硬币找零
You are given coins of different denominations and a total amount of money amount. Write a function ...
- Java for LeetCode 047 Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- Java for LeetCode 214 Shortest Palindrome
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- Java for LeetCode 212 Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
随机推荐
- PI/PO Token配置
接收方通道配置 因为本例中需要在访问业务接口时,获取某平台的TOKEN认证,并在调用业务接口时,将TOKEN一同传给某平台,但是不能放在请求参数中,而是放在HTTP的Headers 注意!是Heade ...
- 用Stream流轻易的收集数据
前言 在日常使用集合时,我们通常使用迭代器来处理集合中的数据,假如有一个用户列表 List,我们想要将用户按照性别分组生成 Map<String, List>.需要遍历 List,然后判断 ...
- HDU 2000 (水)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2000 题目大意:仨字符从小到大排序 解题思路: 很水很水,需要注意的地方是如果用苦力(三个if)要注意 ...
- yum安装mysql 之后问题
日志报错: 190412 15:56:50 [ERROR] Can't open the mysql.plugin table. Please run mysql_upgrade to create ...
- 【Net】CEF浏览IISExpress运行Web项目
前言 本文介绍在Winform桌面应用中,使用IISExpress做Host主机,启动.Net平台的Web项目. 浏览Web网页使用CEF开源组件. 准备 首先创建Winform项目WinFormII ...
- Codeforces1183C(C题)Computer Game
Vova is playing a computer game. There are in total nn turns in the game and Vova really wants to pl ...
- 适配器模式C++实现
目录 类适配器 对象适配器 类适配器 #include <iostream> using namespace std; // Target class Target { public: v ...
- UVA10779 Collectors Problem
题目链接:https://cn.vjudge.net/problem/UVA-10779 前言: 本题是关于姜志豪<网络流的一些建模方法>的笔记. 知识点: 最大流 题意摘抄: \(Bob ...
- 好用的python性能测试神器–Locust
原文链接:https://mp.weixin.qq.com/s/9PxSPuHmucSLi_welq6uNQ 现在性能测试工具太多,根据业务不同使用,比如说我们熟悉的loadrunner.jmeter ...
- 【SpringBoot】SpringBoot 配置这一篇文章就够了
SpringBoot 配置文件 一.配置文件 SpringBoot使用一个全局的配置文件,配置文件名是固定的: application.properties application.yml 配置文件的 ...