leetcode_119 Pascal's Triangle II (Array)

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?

输出杨辉三角的第k行

key:由于只能使用O(k)的空间,只能使用一维数组滚动计算。A[k][n]=A[k-1][n-1]+A[k-1][n]

public class Solution {
    public List<Integer> getRow(int rowIndex) {
        List<Integer> list=new ArrayList<>();
        if (rowIndex==0){
            list.add(0,1);
        }else{
            list.add(0,1); //第一位为1
            for(int i=1;i<=rowIndex;i++){
                list.add(i,1); //第i行的最后一位为1
                for(int j=i-1;j>0;j--){
                    list.set(j,list.get(j)+list.get(j-1));
                }
            }
        }
        return list;
    }
}

由于ArrayList中的 set()和add()方法弄混了,困扰了好久才发现错误。

  

leetcode_119 Pascal's Triangle II (Array)

时间: 2024-10-28 11:47:51

leetcode_119 Pascal's Triangle II (Array)的相关文章

LeetCode_119. Pascal&#39;s Triangle II

119. Pascal's Triangle II Easy Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle. Note that the row index starts from 0. In Pascal's triangle, each number is the sum of the two numbers directly above it. Exa

LeetCode[Array]: 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? 这个问题比较简单. vector<int> getRow(int rowIndex) { vector<int> row, prev

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 解题报告

Pascal's Triangle II Total Accepted: 19384 Total Submissions: 63446 My Submissions Question SolutionGiven 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 us

leetCode 119. Pascal&#39;s Triangle II 数组

119. Pascal'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? 代码如下:(使用双数组处理,未优化版) class Solution { public:     

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]. Note:Could you optimize your algorithm to use only O(k) extra space? 题目标签:Array 这道题目与之前那题不同的地方在于,之前是给我们一个行数n,让我们把这几行的全部写出来,这样就可以在每写新的一行的时候根据之前的那

LeetCode:Pascal‘s Triangle II - 帕斯卡三角形2

1.题目名称 Pascal's Triangle II(帕斯卡三角形2) 2.题目地址 https://leetcode.com/problems/pascals-triangle-ii/ 3.题目内容 英文:Given an index k, return the kth row of the Pascal's triangle. 中文:给出行数k,返回帕斯卡三角形的第k行 例如,k=3时,返回[1,3,3,1] 4.解题方法1 帕斯卡三角形也叫杨辉三角形,在LeetCode第118题(Pas

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