标签:style blog io color os ar for sp div
Given an index k, return the kth row of the Pascal‘s triangle.
For example, given k = 3,
Return [1,3,3,1]
.
Note:
Could you optimize your algorithm to use only O(k) extra space?
1,因为题目中要去用o(k),所以就用本身的arraylist;采用覆盖的方法
2, 注意arraylist的add的用法,
arrlist.add(15); arrlist.add(22); arrlist.add(30); arrlist.add(40); // adding element 25 at third position arrlist.add(2,25);
结果如下:
Number = 15
Number = 22
Number = 25
Number = 30
Number = 40;
3,arraylist的set则会取代原来index的值!!!
public class Solution { public List<Integer> getRow(int rowIndex) { List<Integer> current=new ArrayList<Integer>(); if(rowIndex==0){ current.add(1); return current; } current.add(1); for(int i=1;i<=rowIndex;i++){ current.add(0,1); for(int j=1;j<=i-1;j++){ current.set(j,current.get(j)+current.get(j+1)); } } return current; } }
标签:style blog io color os ar for sp div
原文地址:http://www.cnblogs.com/lilyfindjobs/p/4064168.html