Created
May 12, 2014 08:12
-
-
Save Cee/3b693ab303ab3b1bd9a1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public ArrayList<ArrayList<Integer>> generate(int numRows) { | |
ArrayList<Integer> ret = new ArrayList<Integer>(); | |
ArrayList<Integer> ret_temp = new ArrayList<Integer>(); | |
ArrayList<ArrayList<Integer>> rn = new ArrayList<ArrayList<Integer>>(); | |
if (numRows == 0) return rn; | |
ret_temp.add(1); | |
rn.add(ret_temp); | |
if (numRows == 1) return rn; | |
ret.add(1); | |
ret.add(1); | |
rn.add(ret); | |
if (numRows == 2) return rn; | |
for (int i = 3; i <= numRows; i++){ | |
ret_temp = new ArrayList<Integer>(); | |
ret_temp = ret; | |
ret = new ArrayList<Integer>(); | |
ret.add(1); | |
for (int j = 0; j < ret_temp.size() - 1; j++){ | |
int value = ret_temp.get(j) + ret_temp.get(j + 1); | |
ret.add(value); | |
} | |
ret.add(1); | |
rn.add(ret); | |
} | |
return rn; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Java中ArrayList的add方法
添加的是对象的引用
Example:
ArrayList A = new ArrayList();
ArrayList<ArrayList> B = new ArrayList<ArrayList>();
A.add(1);
B.add(A);//B: [[1]]
A.add(2);//B: [[1, 2]]