1. 程式人生 > >LeetCode_119. 楊輝三角 II

LeetCode_119. 楊輝三角 II

利用雙迴圈,將指定的元素放置在指定的位置。

public class S_119 {
    public List<Integer> getRow(int rowIndex) {
        // 新建列表
        List<Integer> list=new ArrayList<Integer>();
        // 按照行數迴圈
        for(int i=0;i<=rowIndex;i++){
            list.add(1);
            for(int j=i-1;j>=1;j--){
                // 將上兩個的值相加然後放入j的位置
                list.set(j, list.get(j)+list.get(j-1) );
            }
        }
        return list;
    }
}