`
zhang_xzhi_xjtu
  • 浏览: 540041 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

[leetcode] PascalsTriangle2

阅读更多
/**
* <pre>
* 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?
* </pre>
* */
public class PascalsTriangle2 {
    public class Solution {
        public List<Integer> getRow(int rowIndex) {
            List<Integer> result = new ArrayList<Integer>();
            for (int i = 0; i <= rowIndex; i++) {
                result.add(1);
                for (int j = result.size() - 2; j >= 1; j--) {
                    result.set(j, result.get(j) + result.get(j - 1));
                }
            }
            return result;
        }
    }
}
0
5
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics