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

猜你喜欢

转载自blog.csdn.net/king1994wzl/article/details/82787735
今日推荐