119:杨辉三角 II

This commit is contained in:
huangge1199@hotmail.com 2021-04-28 00:08:10 +08:00
parent e30cc3a8f5
commit d3bac1c7f0
2 changed files with 70 additions and 0 deletions

View File

@ -0,0 +1,54 @@
//给定一个非负索引 k其中 k 33返回杨辉三角的第 k
//
//
//
// 在杨辉三角中每个数是它左上方和右上方的数的和
//
// 示例:
//
// 输入: 3
//输出: [1,3,3,1]
//
//
// 进阶
//
// 你可以优化你的算法到 O(k) 空间复杂度吗
// Related Topics 数组
// 👍 281 👎 0
package leetcode.editor.cn;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
//119:杨辉三角 II
public class PascalsTriangleIi {
public static void main(String[] args) {
//测试代码
Solution solution = new PascalsTriangleIi().new Solution();
solution.getRow(3);
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public List<Integer> getRow(int rowIndex) {
List<Integer> list = new ArrayList<>();
list.add(1);
int left = list.get(0);
for (int i = 1; i <= rowIndex; i++) {
for (int j = 1; j < list.size(); j++) {
int temp = list.get(j);
list.add(j, left + list.get(j));
list.remove(j+1);
left = temp;
}
list.add(1);
}
return list;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,16 @@
<p>给定一个非负索引&nbsp;<em>k</em>,其中 <em>k</em>&nbsp;&le;&nbsp;33返回杨辉三角的第 <em>k </em>行。</p>
<p><img alt="" src="https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif"></p>
<p><small>在杨辉三角中,每个数是它左上方和右上方的数的和。</small></p>
<p><strong>示例:</strong></p>
<pre><strong>输入:</strong> 3
<strong>输出:</strong> [1,3,3,1]
</pre>
<p><strong>进阶:</strong></p>
<p>你可以优化你的算法到 <em>O</em>(<em>k</em>) 空间复杂度吗?</p>
<div><div>Related Topics</div><div><li>数组</li></div></div>\n<div><li>👍 281</li><li>👎 0</li></div>