美文网首页
LeetCode 119. Pascal's Trian

LeetCode 119. Pascal's Trian

作者: 关玮琳linSir | 来源:发表于2017-10-08 22:35 被阅读35次

    Given an index k, return the kth row of the Pascal's triangle.

    For example, given k = 3,
    Return [1,3,3,1].

    Note:
    Could you optimize your algorithm to use only O(k) extra space?

    和上一题基本思路一样

    java代码:

    public List<Integer> getRow(int rowIndex) {
            List<List<Integer>> pascal = new ArrayList<List<Integer>>();
            ArrayList<Integer> row = new ArrayList<Integer>();
            for (int i = 0; i < rowIndex+1; i++) {
                row.add(0, 1);
                for (int j = 1; j < row.size() - 1; j++) {
                    row.set(j, row.get(j) + row.get(j + 1));
                }
                pascal.add(new ArrayList<Integer>(row));
            }
            return pascal.get(pascal.size()-1);
        }
    

    相关文章

      网友评论

          本文标题:LeetCode 119. Pascal's Trian

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