LeetCode(#118)————杨辉三角形
生活随笔
收集整理的這篇文章主要介紹了
LeetCode(#118)————杨辉三角形
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
問題描述
給定一個非負整數?numRows,生成楊輝三角的前?numRows?行。
在楊輝三角中,每個數是它左上方和右上方的數的和。
示例:
輸入: 5 輸出: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1] ]實現方法
class Solution {public List<List<Integer>> generate(int numRows) {List<List<Integer>> result = new ArrayList<>();if (numRows < 1) return result;for (int i = 0; i < numRows; i++) {//擴容List<Integer> line = Arrays.asList(new Integer[i+1]);line.set(0, 1); line.set(i, 1);for (int j = 1; j < i; j++) {//等于上一行的左右兩個數字之和line.set(j, result.get(i-1).get(j-1) + result.get(i-1).get(j));}result.add(line);}return result; } }代碼執行結果:
?
總結
以上是生活随笔為你收集整理的LeetCode(#118)————杨辉三角形的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 发那科攻丝回退参数_参数-Fanuc数控
- 下一篇: php登陆项目,ThinkPHP6项目基