LeetCode: Pascal's Triangle II [119]

【题目】

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, k从0开始,返回该索引指向的杨辉三角的行

要求只能使用O(k)的额外空间

【思路】

申请两个k+1大小的数组,交替存储相邻行元素。

杨辉三角特点的生成方法详见Pascal‘s Triangle

【代码】

class Solution {
public:
    vector<int> getRow(int rowIndex) {
        vector<int>result;
        if(rowIndex<0)return result;

        //维护两个rowIndex+1长度的数组, rowIndex为偶数时,行元素存储在row1中;rowIndex为奇数时,行元素存储在row2中
        vector<int> row1(rowIndex+1, 1);
        vector<int> row2(rowIndex+1, 1);
        int indexCount=0;   //当前已经生成的行的行索引
        while(indexCount<rowIndex){
            indexCount++;
            if(indexCount%2==0){
                //当前要从row2生成下一行元素,存到row1中
                //row2有indexCount个元素,row1中有indexCount+1个元素
                for(int i=1; i<indexCount; i++){
                    row1[i]=row2[i-1]+row2[i];
                }
            }
            else{
                //当前要从row1生成下一行元素,存到row2中
                //row1有indexCount个元素,row2中有indexCount+1个元素
                for(int i=1; i<indexCount; i++){
                    row2[i]=row1[i-1]+row1[i];
                }
            }
        }
        //如果rowIndex是偶数,返回row1,否则返回row2
        if(rowIndex%2==0)return row1;
        else return row2;
    }
};

LeetCode: Pascal's Triangle II [119]

时间: 2024-10-17 22:58:51

LeetCode: Pascal's Triangle II [119]的相关文章

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&

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)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? Subscribe to see which companies asked this question 解题分析: 此处有空间的限制,因此不能正

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

题意:给出杨辉三角的层数k,返回最后一层.k=0时就是只有一个数字1. 思路:滚动数组计算前一半出来,返回时再复制另一半.简单但是每一句都挺长的. 1 class Solution { 2 public: 3 vector<int> getRow(int rowIndex) { 4 if(rowIndex==0) return vector<int>(1,1); //0和1特殊处理 5 if(rowIndex==1) return vector<int>(2,1); 6

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

leetcode 119 Pascal&#39;s Triangle II ----- java

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行的数,要求用o(k)的空间复杂度. 也是直接相加就可以了. public class Solution { pub

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

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

C#解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? (注意:这里要求空间为O(k)) 一个满足条件的答案如下: public class Solution { public IList<int