美文网首页
Pascal's Triangle II

Pascal's Triangle II

作者: BLUE_fdf9 | 来源:发表于2018-10-19 23:58 被阅读0次

    题目
    Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle.

    Note that the row index starts from 0.

    答案

    class Solution {
        public List<Integer> getRow(int rowIndex) {
            List<Integer> row = new ArrayList<>();
            row.add(1);
            if(rowIndex == 0) return row;
    
            for(int i = 1; i <= rowIndex; i++) {
                int left = 0;
                int row_size = row.size();
                for(int j = 0; j <= row_size; j++) {
                    int curr = (j < row.size()) ? row.get(j) : 0;
                    if(j < row.size())
                        row.set(j, left + curr);
                    else
                        row.add(left + curr);
                    left = curr;
                }
            }
            return row;
        }
    }
    

    相关文章

      网友评论

          本文标题:Pascal's Triangle II

          本文链接:https://www.haomeiwen.com/subject/gokpzftx.html