6
解决以下问题(帕斯卡三角形)的任务看起来像这样。什么是pascal三角形算法的时间复杂度
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
我已经成功地实施了代码(见下文),但我有一个艰难的时间搞清楚的时间复杂度会是什么这个解决方案。按列表操作的次数是1 + 2 + 3 + 4 + .... + n将操作数减少到n^2数学如何工作并转化为大O符号?
我想这是类似于高斯公式N(N + 1)/ 2,因此为O(n^2),但我可能是错的任何帮助深表感谢
public class Solution {
public List<List<Integer>> generate(int numRows) {
if(numRows < 1) return new ArrayList<List<Integer>>();;
List<List<Integer>> pyramidVal = new ArrayList<List<Integer>>();
for(int i = 0; i < numRows; i++){
List<Integer> tempList = new ArrayList<Integer>();
tempList.add(1);
for(int j = 1; j < i; j++){
tempList.add(pyramidVal.get(i - 1).get(j) + pyramidVal.get(i - 1).get(j -1));
}
if(i > 0) tempList.add(1);
pyramidVal.add(tempList);
}
return pyramidVal;
}
}
感谢您的确认真的很感激它 –