leetcode 441. 排列硬币

你总共有 枚硬币,你需要将它们摆成一个阶梯形状,第 行就必须正好有 枚硬币。

给定一个数字 n,找出可形成完整阶梯行的总行数。

是一个非负整数,并且在32位有符号整型的范围内。

示例 1:

n = 5

硬币可排列成以下几行:
¤
¤ ¤
¤ ¤

因为第三行不完整,所以返回2.

示例 2:

n = 8

硬币可排列成以下几行:
¤
¤ ¤
¤ ¤ ¤
¤ ¤

因为第四行不完整,所以返回3.

思路:每添加一行,硬币数就减小i;当硬币数小于等于0的时候就结束; 最好不要从1开始累加,可能会超出int的范围
 1 class Solution {
 2 public:
 3     int arrangeCoins(int n) {
 4         int sum=0, i=1;
 5         while(n>0){
 6             n -= i;
 7             i++;
 8         }
 9         return n<0?i-2:i-1;
10     }
11 };


原文地址:https://www.cnblogs.com/mr-stn/p/9031239.html

时间: 2024-11-05 22:06:22

leetcode 441. 排列硬币的相关文章

leetcode 441. 排列硬币(Arranging Coins)

目录 题目描述: 示例 1: 示例 2: 解法: 题目描述: 你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状,第 k 行就必须正好有 k 枚硬币. 给定一个数字 n,找出可形成完整阶梯行的总行数. n 是一个非负整数,并且在32位有符号整型的范围内. 示例 1: n = 5 硬币可排列成以下几行: ¤ ¤ ¤ ¤ ¤ 因为第三行不完整,所以返回2. 示例 2: n = 8 硬币可排列成以下几行: ¤ ¤ ¤ ¤ ¤ ¤ ¤ ¤ 因为第四行不完整,所以返回3. 解法: class Soluti

leetcode 441.排列硬币(python)

1.题目描述 你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状,第 k 行就必须正好有 k 枚硬币. 给定一个数字 n,找出可形成完整阶梯行的总行数. n 是一个非负整数,并且在32位有符号整型的范围内. 示例 1: n = 5 硬币可排列成以下几行:¤¤ ¤¤ ¤ 因为第三行不完整,所以返回2.示例 2: n = 8 硬币可排列成以下几行:¤¤ ¤¤ ¤ ¤¤ ¤ 因为第四行不完整,所以返回3. 2. 代码 class Solution: def arrangeCoins(self, n:

Leetcode:Permutations 排列

戳我去解题 Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the following permutations:[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1]. 解题分析:首先进行排序,保证结果保持字典序 class Solution { public: vector<vector<int>

[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 have exactly k coins. Given n, find the total number of full staircase rows that can be formed. n is a non-negative integer and fits within the range of

[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 have exactly k coins. Given n, find the total number of full staircase rows that can be formed. n is a non-negative integer and fits within the range of

[LeetCode] Coin Change 硬币找零

You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins,

[LeetCode] Permutations 排列生成算法之字典序法

字典序排序生成算法 字典序法就是按照字典排序的思想逐一产生所有排列. 例如,由1,2,3,4组成的所有排列,从小到大的依次为: 1234, 1243, 1324, 1342, 1423, 1432, 2134, 2143, 2314, 2341, 2413, 2431, 3124, 3142, 3214, 3241, 3412, 3421, 4123, 4132, 4213, 4231, 4312, 4321. 分析这种过程,看后一个排列与前一个排列之间有什么关系? 再如,设有排列(p)=276

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 have exactly k coins. Given n, find the total number of full staircase rows that can be formed. n is a non-negative integer and fits within the range of

[LeetCode] Coin Path 硬币路径

Given an array A (index starts at 1) consisting of N integers: A1, A2, ..., AN and an integer B. The integer Bdenotes that from any place (suppose the index is i) in the array A, you can jump to any one of the place in the array A indexed i+1, i+2, …