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;
    }
}

你可能感兴趣的:(java,LeetCode,博客,pascal,Triangle)