ice rabbit programming

[LeetCode] Pascal's Triangle 본문

PS/LeetCode

[LeetCode] Pascal's Triangle

판교토끼 2020. 4. 20. 23:58

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

 

Pascal's Triangle - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

윗 열 두 개의 합이 아래 원소가 되는 파스칼 삼각형 문제이다. 정직하게 구현했다.

class Solution {
public:
    vector<vector<int>> generate(int numRows) {
        vector<vector<int>> pascal;
        if(numRows<1)
            return pascal;
        pascal.push_back({1});
        if(numRows==1)
            return pascal;
        pascal.push_back({1,1});
        if(numRows==2)
            return pascal;
        for(int i=2;i<numRows;i++) {
            vector<int> temp;
            temp.push_back(1);
            for(int j=1;j<=i-1;j++)
                temp.push_back(pascal[i-1][j-1]+pascal[i-1][j]);
            temp.push_back(1);
            pascal.push_back(temp);
        }
        return pascal;
    }
};