arranging-coins
https://leetcode.com/problems/arranging-coins/
public class Solution {
public int arrangeCoins(int n) {
// n >= x*(x+1)/2; 2n >= x^2 + x; 8n+1 >= 4x^2 + 4x + 1 = (2x+1)^2
// (8n+1)^(1/2) = 2x+1; x = ((8n+1)^(1/2)-1)/2;
// 注意下面的n要转成long,不然可能溢出
return (int)(Math.sqrt(8*(long)n+1)-1)/2;
}
}
arranging-coins的更多相关文章
- 【leetcode】441. Arranging Coins
problem 441. Arranging Coins solution1: class Solution { public: int arrangeCoins(int n) { ; ; while ...
- Leetcode之二分法专题-441. 排列硬币(Arranging Coins)
Leetcode之二分法专题-441. 排列硬币(Arranging Coins) 你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状,第 k 行就必须正好有 k 枚硬币. 给定一个数字 n,找出可形 ...
- 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] 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: You have a total of n coins that you want to form in a staircase shape, where every k-th ro ...
- [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] 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 ...
- C#LeetCode刷题之#441-排列硬币(Arranging Coins)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3995 访问. 你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状 ...
- 【LeetCode】441. Arranging Coins 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 模拟计算 二分查找 数学公式 日期 题目地址:htt ...
- LeetCode "Arranging Coins"
A simple math.. take care of data overflow though. class Solution { public: int arrangeCoins(int n) ...
随机推荐
- centos6.5安装图形界面,windows远程linux图形界面
1. 查询是否已安装图形界面 yum grouplist |more 在grouplist的输出结果中的“Installed Groups:”部分中,如果你能找到“X Window System”和G ...
- [HTML/CSS]display:none和visibility:hidden的区别
写在前面 在群里有朋友问这样一个问题,display:none的标签,影响了布局.这就引出了本篇这样的问题,印象中display:none的块元素是不占位置的. 一个例子 <!DOCTYPE h ...
- 使用命令行编译、打包、运行WordCount--不用eclipse
1)首先创建WordCount1023文件夹,然后在此目录下使用编辑器,例如vim编写WordCount源文件,并保存为WordCount.java文件 /** * Licensed under th ...
- php调试工具firephp
PHP调试有很多中方法.今天给大家推荐的是 FirePHP 这个工具,是基于firefox的一个组件.Firephp组件安装地址.使用之前需要先安装下这个组件.完了还需要down一个FirePHP的包 ...
- POJ 1488
#include <iostream> #include <string> using namespace std; int main() { string s; int i; ...
- JAVA动态加载JAR包的实现
如何动态的加载这些驱动!不可能把所有的数据库驱动都集成到JAR包中吧?!于是动态加载驱动的JAR包就产生了!其实这些在做系统基础代码时,经常用到,只是一般我们没有机会去搞而已. 动态加载JAR包,使用 ...
- FZU Problem 2148 Moon Game (判断凸四边形)
题目链接 题意 : 给你n个点,判断能形成多少个凸四边形. 思路 :如果形成凹四边形的话,说明一个点在另外三个点连成的三角形内部,这样,只要判断这个内部的点与另外三个点中每两个点相连组成的三个三角形的 ...
- java 语法错误 (操作符丢失) 在查询表达式
遇到的详细问题: a[0]="11"; a[1]="2223"; a[2]="333"; sta.executeUpdate("i ...
- 20个最受欢迎的Linux命令
http://code.csdn.net/news/2819566 1. 以 root 帐户执行上一条命令 sudo !! 2. 利用 Python 搭建一个简单的 Web 服务器,可通过 ht ...
- Tomcat常见内存溢出的解决办法
PermGen space错误解决方法 在看下文之前,首先要确认意见事情,就是你是如何启动tomcat的,我们在平时的开发环境当中,都是通过startup.bat方式启动tomcat的,那么你按照下面 ...