[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ]
含义:要求计算杨辉三角第K行的元素,For example, given k = 3,Return [1,3,3,1]
.
思路:
对任意的n>0有
f(1, n)=1,(n>0)
f(n, n)=1,(n>2)
f(i,j) = f(i-1, j-1)+f(i, j-1),i>2,j>2,
求第k行。
1 public List<Integer> getRow(int rowIndex) { 2 List<Integer> list = new ArrayList<Integer>(); 3 if (rowIndex < 0) 4 return list; 5 6 for (int i = 0; i < rowIndex + 1; i++) { 7 list.add(0, 1); 8 for (int j = 1; j < list.size() - 1; j++) { 9 list.set(j, list.get(j) + list.get(j + 1)); 10 } 11 } 12 return list; 13 }