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?

 

Pascal's Triangle思路相同,只返回最后一行

class Solution {
public:
    vector<int> getRow(int rowIndex) {
        vector<int> cur(1,1);
        vector<int> last = cur;
        for(int i = 1; i <= rowIndex; i ++)
        {// i_th level
            last.push_back(0);
            cur = last;
            for(int j = 1; j <= i; j ++)
            {
                cur[j] = last[j] + last[j-1];
            }
            last = cur;
        }
        return cur;    
    }
};

【LeetCode】119. Pascal's Triangle II

相关文章:

  • 2022-12-23
  • 2021-07-20
  • 2021-12-04
猜你喜欢
  • 2021-04-28
  • 2021-04-17
  • 2022-01-18
  • 2021-06-22
  • 2021-10-02
  • 2021-09-12
  • 2021-10-23
相关资源
相似解决方案