[leedcode 119] 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]
.
public class Solution { public List<Integer> getRow(int rowIndex) { //由于需要O(k)空间,因此需要借助两个数组保存中间值,并交换两个数组,注意交换的方法! List<Integer> list=new ArrayList<Integer>(); List<Integer> list1=new ArrayList<Integer>(); if(rowIndex<0) return list; for(int i=0;i<=rowIndex;i++){ for(int j=0;j<=i;j++){ if(j==0||j==i) list1.add(1); else{ list1.add(list.get(j)+list.get(j-1)); } } List<Integer> temp=list;///// list=list1; list1=temp; list1.clear(); } return list; } }