//给定一个非负索引 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)
}
【leetcode】P119杨辉三角II
最新推荐文章于 2024-01-18 18:44:56 发布