Pascal's Triangle II 解答

Question

Given an index k, return the kth row of the Pascal‘s triangle.

For example, given k = 3,
Return [1,3,3,1].

Solution

Similar with Pascal‘s Triangle. Note that the index starts from 0.

 1 public class Solution {
 2     public List<Integer> getRow(int rowIndex) {
 3         List<Integer> result = new ArrayList<Integer>();
 4         List<Integer> prev = new ArrayList<Integer>();
 5         if (rowIndex < 0)
 6             return result;
 7         result.add(1);
 8         prev.add(1);
 9         while (rowIndex > 0) {
10             result = new ArrayList<Integer>();
11             result.add(1);
12             int length = prev.size();
13             for (int i = 0; i < length - 1; i++)
14                 result.add(prev.get(i) + prev.get(i + 1));
15             result.add(1);
16             prev = result;
17             rowIndex--;
18         }
19         return result;
20     }
21 }

We can also only use one list.

 1 public List<Integer> getRow(int rowIndex) {
 2     ArrayList<Integer> result = new ArrayList<Integer>();
 3
 4     if (rowIndex < 0)
 5         return result;
 6
 7     result.add(1);
 8     for (int i = 1; i <= rowIndex; i++) {
 9         for (int j = result.size() - 2; j >= 0; j--) {
10             result.set(j + 1, result.get(j) + result.get(j + 1));
11         }
12         result.add(1);
13     }
14     return result;
15 }

Pascal's Triangle II 解答

时间: 2024-09-30 16:40:47

Pascal's Triangle II 解答的相关文章

125.Pascal&#39;s Triangle II

题目: Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle. 给定非负索引k,其中k≤33,返回Pascal三角形的第k个索引行. Note that the row index starts from 0. 请注意,行索引从0开始. In Pascal's triangle, each number is the sum of the two numbers d

119. Pascal&#39;s Triangle II

Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to use only O(k) extra space? 1 class Solution { 2 public: 3 vector<int> getRow(int rowIndex) { 4 5 vect

Java [Leetcode 119]Pascal&#39;s Triangle II

题目描述: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. 解题思路: 每次在上一个list前面插入1,然后后面的每两个间相加赋值给前一个数. 代码描述: public class Solution { public List<Integer> getRow(int rowIndex) { List<Integer> r

[leedcode 119] Pascal&#39;s Triangle II

Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. public class Solution { public List<Integer> getRow(int rowIndex) { //由于需要O(k)空间,因此需要借助两个数组保存中间值,并交换两个数组,注意交换的方法! List<Integer> list=new

LeetCode——Pascal&#39;s Triangle II

Description: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. public class Solution { public List<Integer> getRow(int rowIndex) { List<List<Integer>> list = new ArrayList<List&

Pascal&#39;s Triangle II 杨辉三角

1 class Solution { 2 public: 3 vector<int> getRow(int rowIndex) { 4 vector<vector<int>> tri; 5 if(rowIndex==0) 6 { 7 vector<int> c; 8 c.push_back(1); 9 return c; 10 } 11 vector<int> b; 12 b.push_back(0); 13 b.push_back(1); 14

Pascal&#39;s Triangle II

Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? Hide Tags Array vector<int> getRow(int rowIndex) { vector<int> res

leetcode 【 Pascal&#39;s Triangle II 】python 实现

题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to use only O(k) extra space? 代码:oj测试通过 Runtime: 48 ms 1 class Solution: 2 # @return a list of intege

LeetCode119 Pascal&#39;s Triangle II

Given an index k, return the kth row of the Pascal's triangle. (Easy) For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to use only O(k) extra space? 分析: 上一题的followup,注意只能开O(k)的空间,实际上就是利用滚动数组的解法,每一行只与其上一行有关. 代码: 1 clas