暑假开始的第一题,。,。先找找easy的找回手感哈哈,练习一下使用的junit
思路:保留每一次的子item方便下一次使用就行了,,,除了遍历暂时不知道更加合理的方法
public class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> result = new ArrayList<>();
List<Integer> last = new ArrayList<>();
if (numRows != 0) {
for (int i = 0; i < numRows; i++) {
List<Integer> item = new ArrayList<>();
if (i == 0) {
item.add(1);
} else if (i == 1) {
item.add(1);
item.add(1);
} else if (i > 1) {
item.add(1);
for (int j = 0; j < last.size() - 1; j++) {
item.add(last.get(j) + last.get(j + 1));
}
item.add(1);
}
result.add(item);
last = item;
}
}
return result;
}
public void print(List<List<Integer>> temp) {
for (List<Integer> list : temp) {
for (Integer interger : list) {
System.out.print(interger + " ");
}
System.out.println();
}
}
}
注意处理和测试用例的输入格式就行,因为List的输出null和[]是不一样的