Problem Pascal's Triangle II

Problem Description:

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?

Solution:

 1     public List<Integer> getRow(int rowIndex) {
 2 List<Integer> row = new ArrayList<Integer>();
 3         List<Integer> preRow = new ArrayList<Integer>();
 4         for (int i = 0; i <= rowIndex; i++) {
 5             preRow.add(1);
 6             row.add(1);
 7         }
 8             for (int i = 0; i <= rowIndex; i++) {
 9             for (int j = 0; j <= i; j++) {
10                 if (j == 0 || j == i) {
11                     row.set(j, 1);
12                 } else {
13                     row.set(j, preRow.get(j-1)+preRow.get(j));
14                 }
15             }
16             List<Integer> temp = preRow;
17             preRow = row;
18             row = temp;
19         }
20
21         return preRow;
22     }

Problem Pascal's Triangle II

时间: 2024-07-30 01:53:46

Problem Pascal's Triangle II的相关文章

LeetCode#119 Pascal&#39;s Triangle II

Problem Definition: 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? Solution: 1 def getRow(rowIndex): 2 pt=[] 3 for rn in

leetcode || 119、Pascal&#39;s Triangle II

problem: 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 题意:输出杨辉三角形的第K层 即:第K+1行 thinking: 题目要求使用O(K)的额外空间

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

Pascal&#39;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(in

[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