国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

LeetCode -- Arranging Coins

2019-11-08 02:10:57
字體:
來源:轉載
供稿:網友
題目描述: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 a 32-bit signed integer.Example 1:n = 5The coins can form the following rows:¤¤ ¤¤ ¤Because the 3rd row is incomplete, we return 2.其實就是求小于N的最大M,M為1+...+K。思路:遍歷1到n/2即可。實現代碼:
public int ArrangeCoins(int n) {        if(n == 0){            return 0;        }        if(n == 1){            return 1;        }                        var half = n/2 + 1;                var sum = 0;		var count = 0;        for(var i = 1;sum >= 0 && i <= half && sum < n; i++){            sum += i;			count ++;        }        if(sum > n || sum < 0){			count --;		}		//Console.WriteLine(sum);        return count;    }
上一篇:文章標題

下一篇:LeetCode 63. Unique Paths II

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 无极县| 耒阳市| 南木林县| 麟游县| 永和县| 泰来县| 安远县| 武汉市| 武安市| 铅山县| 苗栗县| 栾城县| 松江区| 林甸县| 武城县| 旺苍县| 通州区| 洛川县| 连南| 日土县| 萨迦县| 齐河县| 连州市| 鸡西市| 虞城县| 吴堡县| 顺义区| 长子县| 梓潼县| 昭通市| 万山特区| 龙门县| 贺州市| 高尔夫| 卢龙县| 方正县| 华容县| 福泉市| 霍州市| 佛教| 沭阳县|