码迷,mamicode.com
首页 > 其他好文 > 详细

arranging-coins

时间:2016-10-31 14:04:24      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:range   https   return   span   long   com   math   blog   log   

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

标签:range   https   return   span   long   com   math   blog   log   

原文地址:http://www.cnblogs.com/charlesblc/p/6015435.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!