Skip to content

Latest commit

 

History

History
28 lines (21 loc) · 909 Bytes

LeetCodeSolution.md

File metadata and controls

28 lines (21 loc) · 909 Bytes

LeetCode Problems Solution:

Problem 1: Given an integer numRows, return the first numRows of Pascal's triangle.

In Pascal's triangle, each number is the sum of the two numbers directly

Example 1:

Input: numRows = 5 Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]

Solution:

        class Solution {
        public:
            vector<vector<int>> generate(int numRows) {
                std::vector<std::vector<int>> result;
                for(int i=0; i < numRows; i++){
                    std::vector<int> row(i + 1, 1);
                    for(int j = 1; j < i; j++){
                        row[j] = result[i - 1][j - 1] + result[i - 1][j];
                    }
                    result.push_back(row);
                }
                return result;
            }
        };