z_zhaojun的博客

机会只会留给有准备的人

leetcode:119. Pascal's Triangle II(Java)解答

转载请注明出处:z_zhaojun的博客
原文地址:http://blog.csdn.net/u012975705/article/details/50493006
题目地址:https://leetcode.com/problems/pascals-triangle-ii/

Pascal’s Triangle II

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

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

解法(Java):

public class Solution {
    public List<Integer> getRow(int rowIndex) {
        if (rowIndex < 0) {
            return null;
        }
        rowIndex++;
        List<Integer> list = new ArrayList<>();
        list.add(1);
        for (int row = 0; row < rowIndex; row++) {
            int tmp = 1;
            for (int col = 1; col <= row; col++) {
                if (col < list.size()) {
                    int tmp2 = list.get(col);
                    list.set(col, tmp + list.get(col));
                    tmp = tmp2;
                } else {
                    list.add(1);
                }
            }
        }
        return list;
    }
}
阅读更多
版权声明:本文为博主原创文章,转载请注明出处。博客首页:http://blog.csdn.net/u012975705。订阅:http://blog.csdn.net/u012975705/rss/list https://blog.csdn.net/u012975705/article/details/50493006
个人分类: LeetCode 算法相关
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭