반응형

https://leetcode.com/problems/pascals-triangle-ii/

 

Pascal's Triangle II - LeetCode

Can you solve this real interview question? Pascal's Triangle II - Given an integer rowIndex, return the rowIndexth (0-indexed) row of the Pascal's triangle. In Pascal's triangle, each number is the sum of the two numbers directly above it as shown: [https

leetcode.com

 

class Solution {
public:
    vector<int> getRow(int rowIndex) {
        vector<int> res(rowIndex + 1, 1);
        for(int i=2; i<=rowIndex; i++) {
            for(int j=i - 1; j>=1; j--)
                res[j] = res[j - 1] + res[j];
        }
        return res;
    }
};
반응형

+ Recent posts