///给出一个非负索引 k,其中 k ≤ 33.返回杨辉三角 k 行。 // // // // 在杨辉三角中,每个数字都是左上角和右上角的数字和。 // // 示例: // // 输入: 3 //输出: [1,3,3,1] // // // 进阶: // // 你可以优化你的算法 O(k) 空间复杂吗? // Related Topics 数组 // ?? 195 ?? 0 package leetcode.editor.cn; import java.util.ArrayList; import java.util.List; //Java:杨辉三角 II public class P119PascalsTriangleIi {
public static void main(String[] args) {
Solution solution = new P119PascalsTriangleIi().new Solution(); // TO TEST List<Integer> row = solution.getRow(3); System.out.println(row); } //leetcode submit region begin(Prohibit modification and deletion) class Solution {
public List<Integer> getRow(int rowIndex) {
ArrayList<Integer> res = new ArrayList<>(rowIndex 1); if (rowIndex< 0)
return res;
res.add(1);
for (int i = 1; i <= rowIndex; i++) {
for (int j = i - 1; j > 0; j--) {
res.set(j,res.get(j)+res.get(j-1));
}
res.add(1);
}
return res;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}