Algorithm


Problem Nmae: 119. Pascal's Triangle II

problem Link: https://leetcode.com/problems/pascals-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:

 

Example 1:

Input: rowIndex = 3
Output: [1,3,3,1]

Example 2:

Input: rowIndex = 0
Output: [1]

Example 3:

Input: rowIndex = 1
Output: [1,1]

Code Examples

#1 Code Example with C Programming

Code - C Programming


#include <stdio.h>
#include <stdlib.h>

int *getRow(int rowIndex) {
    int k = rowIndex;
    int *ans = (int *)calloc(k + 1, sizeof(int));
    int i, j;
    int prev = 0, cur = 0;
    ans[0] = 1;
    for (i = 1; i  <  k + 1; i++) {
        prev = ans[0];
        for (j = 1; j  <  i; j++) {
            cur = ans[j];
            ans[j] += prev;
            prev = cur;
        }
        ans[i] = 1;
    }
    return ans;
}

int main() {
    int k = 5;
    int *ans = getRow(k);
    int i;
    for (i = 0; i  <  k + 1; i++) {
        printf("%d ", ans[i]);
    }
    printf("\n");
    return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
[1,3,3,1]

Output

x
+
cmd
[1,3,3,1]

#2 Code Example with C++ Programming

Code - C++ Programming


class Solution {
public:
    vector<int> getRow(int rowIndex) {
        if(rowIndex == 0) return {1};
        auto v = getRow(rowIndex - 1);
        int n = v.size();
        for(int i = 1; i < n; i++)
            v[i] = (i  < = n/2) ? v[i] + v[n - i] : v[n - i];
        v.push_back(1>;
        return v;
    }
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
[1,3,3,1]

Output

x
+
cmd
[1,3,3,1]

#3 Code Example with Java Programming

Code - Java Programming


class Solution {
  public List getRow(int rowIndex) {
    List lastRow = new ArrayList<>();
    for (int i = 0; i  < = rowIndex; i++) {
      List copy = new ArrayList<>(lastRow);
      lastRow.clear();
      for (int j = 0; j  < = i; j++) {
        if (j == 0 || j == i) {
          lastRow.add(1);
        } else {
          lastRow.add(copy.get(j - 1) + copy.get(j));
        }
      }
    }
    return lastRow;
  }   
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
rowIndex = 0

Output

x
+
cmd
[1]

#4 Code Example with Javascript Programming

Code - Javascript Programming


const getRow = function(rowIndex) {
  if (!rowIndex) return [1];
  if (rowIndex === 1) return [1, 1];
  const res = [1, 1];
  for (let i = 2; i  < = rowIndex; i++) {
    res[i] = 1;
    for (let j = i - 1; j >= 1; j--) {
      res[j] = res[j] + res[j - 1];
    }
  }
  return res;
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
rowIndex = 0

Output

x
+
cmd
[1]

#5 Code Example with Python Programming

Code - Python Programming


class Solution:
    def getRow(self, rowIndex: int, row = [1]) -> List[int]:
        return self.getRow(rowIndex - 1, [a + b for a, b in zip([0] + row, row + [0])]) if rowIndex else row
Copy The Code & Try With Live Editor

Input

x
+
cmd
rowIndex = 1

Output

x
+
cmd
[1,1]

#6 Code Example with C# Programming

Code - C# Programming


using System.Collections.Generic;

namespace LeetCode
{
    public class _119_PascalsTriangle2
    {
        public IList < int> GetRow(int rowIndex)
        {
            var result = new int[rowIndex + 1];
            result[0] = 1;
            if (rowIndex == 0) return result;

            for (int i = 1; i  < = rowIndex; i++)
                for (int j = i; j > 0; j--)
                    result[j] = result[j - 1] + result[j];

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

Input

x
+
cmd
rowIndex = 1

Output

x
+
cmd
[1,1]
Advertisements

Demonstration


Previous
#118 Leetcode Pascal's Triangle Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#120 Leetcode Triangle Solution in C, C++, Java, JavaScript, Python, C# Leetcode