leetcode Pascal's Triangle II

Pascal's Triangle II  杨辉三角 要求空间复杂度为O(k)

解题思路:

1.从后向前遍历 

2.使用list.set 更新指定索引的值

public static void main(String[] args) {
		List<Integer> row = getRow(3);
		System.out.println(row);

	}
	public  static List<Integer> getRow(int rowIndex) {
		List<Integer> list=new ArrayList<>();
		list.add(1);
		for(int i=1;i<=rowIndex;i++){
			for(int j=list.size()-2;j>=0;j--){
				list.set(j+1,list.get(j)+list.get(j+1));
			}
			list.add(1);
		}
		return list;
	}

猜你喜欢

转载自blog.csdn.net/u011243684/article/details/84796553