Algorithm


Problem Nmae: 118. Pascal's Triangle

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

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 above it as shown:

 

Example 1:

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

Example 2:

Input: numRows = 1
Output: [[1]]

 

Constraints:

  • 1 <= numRows <= 30
 

Code Examples

#1 Code Example with C Programming

Code - C Programming


int** generate(int numRows, int** columnSizes) {
     int i, j;
     int *buff, **p;
     p = malloc(numRows * sizeof(int *));
     *columnSizes = malloc(numRows * sizeof(int));
     //assert(p && *columnSizes);
     for (i = 1; i  < = numRows; i ++) {
         buff = malloc(i * sizeof(int));
         //assert(buff);
         p[i - 1] = buff;
          (*columnSizes)[i - 1] = i;
         buff[0] = 1;
         for (j = 1; j  <  i - 1; j ++) {
            buff[j] = p[i - 2][j - 1] + p[i - 2][j];
          }
         buff[i - 1] = 1;
     }
     return p;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
numRows = 5

Output

x
+
cmd
[[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]

#2 Code Example with C++ Programming

Code - C++ Programming


class Solution {
public:
    vector<vector<int>> generate(int numRows) {
        if(numRows == 0) return {};
        if(numRows == 1) return {{1}};
        auto v = generate(numRows - 1);
        auto lastRow = *(v.end() - 1);
        vector<int>res(1, 1);
        for(int i = 0; i < lastRow.size() - 1; i++) res.push_back(lastRow[i] + lastRow[i + 1]);
        res.push_back(1);
        v.push_back(res>;
        return v;
    }
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
numRows = 5

Output

x
+
cmd
[[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]

#3 Code Example with Java Programming

Code - Java Programming


class Solution {
  public List();
    for (int i = 0; i  <  numRows; i++) {
      List temp = new ArrayList<>();
      for (int j = 0; j  < = i; j++) {
        temp.add(
          (j == 0 || j == i) ? 1 : 
          (result.get(i - 1).get(j - 1) + result.get(i - 1).get(j)));
      }
      result.add(temp);
    }
    return result;
  }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
numRows = 1

Output

x
+
cmd
[[1]]

#4 Code Example with Javascript Programming

Code - Javascript Programming


const generate = function(numRows) {
  // row 0 => [1] length 0
  // row 1 => [1, 1] length 1
  // row 2 => [1, 2, 1] length 2
  // row 3 => [1, 3, 3, 1] length 3

  // current[i] = prev[i - 1] + prev[i]

  const res = [];
  for (let row = 0; row  <  numRows; row += 1) {
    if (row === 0) {
      res.push([1]);
      continue;
    }

    if (row === 1) {
      res.push([1, 1]);
      continue;
    }

    const newRow = [];
    const maxIdx = row;
    for (let i = 0; i  < = maxIdx; i += 1) {
      if (i === 0 || i === maxIdx) {
        newRow.push(1);
      } else {
        newRow.push(res[row - 1][i - 1] + res[row - 1][i]);
      }
    }
    res.push(newRow);
  }

  return res;
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
numRows = 1

Output

x
+
cmd
[[1]]

#5 Code Example with Python Programming

Code - Python Programming


class Solution:
    def generate(self, numRows: int) -> List[List[int]]:
        res = numRows and [[1]] or []
        for _ in range(numRows-1):
            res.append([1] + [a + b for a, b in zip(res[-1], res[-1][1:])] + [1])
        return res
Copy The Code & Try With Live Editor

Input

x
+
cmd
numRows = 5

Output

x
+
cmd
[[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]

#6 Code Example with C# Programming

Code - C# Programming


using System.Collections.Generic;

namespace LeetCode
{
    public class _118_PascalsTriangle
    {
        public IList < IList<int>> Generate(int numRows)
        {
            var results = new List < IList<int>>();
            if (numRows  < = 0) return results;
            results.Add(new List<int>(1) { 1 });
            if (numRows == 1) return results;
            results.Add(new List < int>(2) { 1, 1 });
            if (numRows == 2) return results;

            for (int i = 2; i  <  numRows; i++)
            {
                var result = new List<int>(i + 1);
                result.Add(1);
                for (int j = 1; j  <  i; j++)
                {
                    result.Add(results[i - 1][j - 1] + results[i - 1][j]);
                }
                result.Add(1);
                results.Add(result);
            }
            return results;
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
numRows = 5

Output

x
+
cmd
[[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
Advertisements

Demonstration


Previous
#117 Leetcode Populating Next Right Pointers in Each Node II Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#119 Leetcode Pascal's Triangle II Solution in C, C++, Java, JavaScript, Python, C# Leetcode