0
点赞
收藏
分享

微信扫一扫

LeetCode-119-杨辉三角 II

程序员知识圈 2021-09-28 阅读 156
LeetCode

杨辉三角 II

解法一:暴力破解法
import java.util.ArrayList;
import java.util.List;

public class LeetCode_119 {
    public static List<Integer> getRow(int rowIndex) {
        List<Integer> one = new ArrayList<>();
        one.add(1);
        if (rowIndex == 0) {
            return one;
        }
        List<Integer> two = new ArrayList<>();
        two.add(1);
        two.add(1);
        if (rowIndex == 1) {
            return two;
        }
        List<Integer> last = two;
        List<Integer> cur = new ArrayList<>();
        for (int i = 2; i <= rowIndex; i++) {
            cur = new ArrayList<>();
            cur.add(1);
            for (int j = 1; j < i; j++) {
                cur.add(last.get(j - 1) + last.get(j));
            }
            cur.add(1);
            last = cur;
        }
        return cur;
    }

    public static void main(String[] args) {
        for (Integer integer : getRow(3)) {
            System.out.print(integer + " ");
        }
    }
}
举报

相关推荐

0 条评论