領釦LintCode演算法問題答案-1354. 楊輝三角形II
領釦LintCode演算法問題答案-1354. 楊輝三角形II
1354. 楊輝三角形II
描述
給定非負索引k,其中k≤33,返回楊輝三角形的第k個索引行。
- 注意行下標從 0 開始
- 在楊輝三角中,每個數字是它上面兩個數字的總和。
樣例 1:
輸入: 3
輸出: [1,3,3,1]
樣例 2:
輸入: 4
輸出: [1,4,6,4,1]
題解
public class Solution {
/**
* @param rowIndex: a non-negative index
* @return: the kth index row of the Pascal's triangle
*/
public List<Integer> getRow(int rowIndex) {
// write your code here
int[] a = new int[rowIndex + 1];
a[0] = 1;
int row = 1;
while (row++ <= rowIndex) {
a[row - 1] = 1;
for (int i = row - 2; i >= 1; i--) {
a[i] = a[i] + a[i - 1];
}
}
List<Integer> ret = new ArrayList<>();
for (int n : a) {
ret.add(n);
}
return ret;
}
}
鳴謝
非常感謝你願意花時間閱讀本文章,本人水平有限,如果有什麼說的不對的地方,請指正。
歡迎各位留言討論,希望小夥伴們都能每天進步一點點。
相關文章
- 領釦LintCode演算法問題答案-1343. 兩字串和演算法字串
- 領釦LintCode演算法問題答案-988. 硬幣擺放演算法
- 領釦LintCode演算法問題答案-1895. 安排面試城市演算法面試
- 領釦LintCode演算法問題答案-77. 最長公共子序列演算法
- 領釦LintCode演算法問題答案-1878. 旋轉數字演算法
- 領釦LintCode演算法問題答案-1886. 目標移動演算法
- 領釦LintCode演算法問題答案-1320. 包含重複值演算法
- 領釦LintCode演算法問題答案-1225. 島的周長演算法
- 領釦LintCode演算法問題答案-1876. 外星人字典(簡單)演算法
- 領釦LintCode演算法問題答案-1214. 許可證金鑰格式演算法
- 領釦LintCode演算法問題答案-1206. 下一個更大的數 I演算法
- 楊輝三角形
- LintCode 主元素 II
- LintCode-BackPack II
- 使用python生成楊輝三角形Python
- LintCode-Maximum Subarray II
- LintCode-Word Search II
- LintCode-Majority Number II
- LintCode-Median II
- LintCode-Sort Colors II
- LintCode-Unique Path II
- LintCode - A + B 問題(普通)
- 楊輝三角形 (用陣列和遞迴)陣列遞迴
- SAP面試問題答案面試
- LintCode-Search 2D Matrix II
- 史上最簡單的 《三角形判定》 面試題答案面試題
- Python計算組合數生成楊輝三角形Python
- 前端演算法題:JS遞迴實現楊輝三角前端演算法JS遞迴
- LintCode/LeetCode訓練題目&答案詳解—基礎篇LeetCode
- 符號三角形問題符號
- lintcode演算法題 落單的數 JavaScript演算法JavaScript
- Java實現-揹包問題IIJava
- 老虎SQL問題答案之漏洞SQL
- (轉)ABAP 面試問題及答案面試
- 【Lintcode】398. Longest Continuous Increasing Subsequence II
- Python楊輝三角演算法Python演算法
- 【LeetCode回溯演算法#07】子集問題I+II,鞏固解題模板並詳解回溯演算法中的去重問題LeetCode演算法
- [LintCode] Parking Lot 停車場問題