leetcode——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?

Subscribe to see which companies asked this question

class Solution {
 public:
     vector<int> getRow(int rowIndex) {
        
         vector<int> a(rowIndex + 1);
         
         a[0] = 1;
         for(int i = 1; i <= rowIndex; i++)
             for(int j = i; j >= 0; j--)
                 if (j == i)
                     a[j] = a[j-1];
                 else if (j == 0)
                     a[j] = a[j];
                 else
                     a[j] = a[j-1] + a[j];
                     
         return a;                    
     }
 };

你可能感兴趣的:(LeetCode,算法题)