zoukankan      html  css  js  c++  java
  • [LeetCode] 441. Arranging Coins

    You have n coins and you want to build a staircase with these coins. The staircase consists of k rows where the ith row has exactly i coins. The last row of the staircase may be incomplete.

    Given the integer n, return the number of complete rows of the staircase you will build.

    Example 1:

    Input: n = 5
    Output: 2
    Explanation: Because the 3rd row is incomplete, we return 2.
    

    Example 2:

    Input: n = 8
    Output: 3
    Explanation: Because the 4th row is incomplete, we return 3.

    Constraints:

    • 1 <= n <= 231 - 1

    排列硬币。

    你总共有 n 枚硬币,并计划将它们按阶梯状排列。对于一个由 k 行组成的阶梯,其第 i 行必须正好有 i 枚硬币。阶梯的最后一行 可能 是不完整的。

    给你一个数字 n ,计算并返回可形成 完整阶梯行 的总行数。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/arranging-coins
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    我给出一个网友的思路

    时间O(1)

    空间O(1)

    Java实现

    1 class Solution {
    2     public int arrangeCoins(int n) {
    3         return (int) (Math.sqrt((double) 2 * n + 0.25) - 0.5);
    4     }
    5 }

    JavaScript实现

    1 /**
    2  * @param {number} n
    3  * @return {number}
    4  */
    5 var arrangeCoins = function (n) {
    6     return Math.floor(Math.sqrt(2 * n + 1 / 4) - 1 / 2);
    7 };

    LeetCode 题目总结

  • 相关阅读:
    对SpringIOC、AOP的理解
    Java后台与VUE跨域交接
    贼简单的Shiro框架之粗粒度控制菜单栏
    Json
    Spring MVC小DEMO
    面试问题
    多线程理解
    了解java语言
    单点登录如何设计
    进程的创建和调度分析
  • 原文地址:https://www.cnblogs.com/cnoodle/p/13222616.html
Copyright © 2011-2022 走看看