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:

0 <= rowIndex <= 33No developer has claimed the Python throne for LeetCode #119 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
Each element in a row of Pascal's triangle can be calculated from the previous element in the same row using combinatorics.
The k-th element of the n-th row is given by the combination formula C(n, k).
We can compute C(n, k) based on C(n, k-1) by multiplying it by (n - k + 1) and then dividing by k.
This allows us to generate the entire row in a single pass without needing to compute previous rows.
class Solution:
def getRow(self, rowIndex: int) -> List[int]:
# Initialize the result array with 1s.
# This handles the boundary 1s automatically.
res = [1] * (rowIndex + 1)
# Calculate intermediate values using the combination formula
for k in range(1, rowIndex):
# res[k] = res[k-1] * (rowIndex - k + 1) / k
# Use integer division // to ensure we keep integers
res[k] = res[k - 1] * (rowIndex - k + 1) // k
return res
No developer has claimed the Java throne for LeetCode #119 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
Each element in a row of Pascal's triangle can be calculated from the previous element in the same row using combinatorics.
The k-th element of the n-th row is given by the combination formula C(n, k).
We can compute C(n, k) based on C(n, k-1) by multiplying it by (n - k + 1) and then dividing by k.
This allows us to generate the entire row in a single pass without needing to compute previous rows.
class Solution {
public List<Integer> getRow(int rowIndex) {
List<Integer> res = new ArrayList<>();
// The first element is always 1
res.add(1);
// Use long to prevent integer overflow during multiplication
long prev = 1;
for (int k = 1; k <= rowIndex; k++) {
// Calculate next combination value: C(n, k) = C(n, k-1) * (n - k + 1) / k
long curr = prev * (rowIndex - k + 1) / k;
res.add((int) curr);
prev = curr;
}
return res;
}
}
No developer has claimed the Javascript throne for LeetCode #119 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
Each element in a row of Pascal's triangle can be calculated from the previous element in the same row using combinatorics.
The k-th element of the n-th row is given by the combination formula C(n, k).
We can compute C(n, k) based on C(n, k-1) by multiplying it by (n - k + 1) and then dividing by k.
This allows us to generate the entire row in a single pass without needing to compute previous rows.
/**
* @param {number} rowIndex
* @return {number[]}
*/
var getRow = function(rowIndex) {
// Initialize array of size rowIndex + 1 with 1s
const res = new Array(rowIndex + 1).fill(1);
let prev = 1;
// Calculate the values between the boundary 1s
for (let k = 1; k < rowIndex; k++) {
// Numbers in JS are double precision floats, safe up to 2^53 - 1
// The max value in Pascal's triangle for rowIndex 33 is ~1.16 * 10^9, so it's perfectly safe
let curr = (prev * (rowIndex - k + 1)) / k;
res[k] = curr;
prev = curr;
}
return res;
};
No developer has claimed the Cpp throne for LeetCode #119 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
Each element in a row of Pascal's triangle can be calculated from the previous element in the same row using combinatorics.
The k-th element of the n-th row is given by the combination formula C(n, k).
We can compute C(n, k) based on C(n, k-1) by multiplying it by (n - k + 1) and then dividing by k.
This allows us to generate the entire row in a single pass without needing to compute previous rows.
class Solution {
public:
vector<int> getRow(int rowIndex) {
// Initialize vector with size rowIndex + 1 and default value 1
vector<int> res(rowIndex + 1, 1);
long long prev = 1;
// Iterate through the inner elements
for (int k = 1; k < rowIndex; ++k) {
// Use long long to avoid overflow when multiplying large numbers
long long curr = prev * (rowIndex - k + 1) / k;
res[k] = curr;
prev = curr;
}
return res;
}
};
No developer has claimed the Csharp throne for LeetCode #119 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
Each element in a row of Pascal's triangle can be calculated from the previous element in the same row using combinatorics.
The k-th element of the n-th row is given by the combination formula C(n, k).
We can compute C(n, k) based on C(n, k-1) by multiplying it by (n - k + 1) and then dividing by k.
This allows us to generate the entire row in a single pass without needing to compute previous rows.
public class Solution {
public IList<int> GetRow(int rowIndex) {
int[] res = new int[rowIndex + 1];
// Set the first element
res[0] = 1;
long prev = 1;
for (int k = 1; k <= rowIndex; k++) {
// Compute the next element using the combination formula
// We use 'long' to prevent multiplication overflow
long curr = prev * (rowIndex - k + 1) / k;
res[k] = (int)curr;
prev = curr;
}
return res;
}
}
No developer has claimed the Go throne for LeetCode #119 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
Each element in a row of Pascal's triangle can be calculated from the previous element in the same row using combinatorics.
The k-th element of the n-th row is given by the combination formula C(n, k).
We can compute C(n, k) based on C(n, k-1) by multiplying it by (n - k + 1) and then dividing by k.
This allows us to generate the entire row in a single pass without needing to compute previous rows.
func getRow(rowIndex int) []int {
// Initialize the slice with the required size
res := make([]int, rowIndex + 1)
res[0] = 1
prev := int64(1)
for k := 1; k <= rowIndex; k++ {
// Calculate current element using int64 to prevent overflow
curr := prev * int64(rowIndex - k + 1) / int64(k)
res[k] = int(curr)
prev = curr
}
return res
}
No developer has claimed the Ruby throne for LeetCode #119 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
Each element in a row of Pascal's triangle can be calculated from the previous element in the same row using combinatorics.
The k-th element of the n-th row is given by the combination formula C(n, k).
We can compute C(n, k) based on C(n, k-1) by multiplying it by (n - k + 1) and then dividing by k.
This allows us to generate the entire row in a single pass without needing to compute previous rows.
# @param {Integer} row_index
# @return {Integer[]}
def get_row(row_index)
# Create array initialized with 1s
res = Array.new(row_index + 1, 1)
prev = 1
# Calculate the inner values
(1...row_index).each do |k|
# Ruby automatically handles arbitrarily large integers, so no overflow concerns
curr = prev * (row_index - k + 1) / k
res[k] = curr
prev = curr
end
res
end
No developer has claimed the Rust throne for LeetCode #119 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
Each element in a row of Pascal's triangle can be calculated from the previous element in the same row using combinatorics.
The k-th element of the n-th row is given by the combination formula C(n, k).
We can compute C(n, k) based on C(n, k-1) by multiplying it by (n - k + 1) and then dividing by k.
This allows us to generate the entire row in a single pass without needing to compute previous rows.
impl Solution {
pub fn get_row(row_index: i32) -> Vec<i32> {
// Initialize vector with 1s
let mut res = vec![1; (row_index + 1) as usize];
let mut prev: i64 = 1;
for k in 1..row_index {
// Use i64 for calculation to avoid integer overflow
let curr = prev * (row_index as i64 - k as i64 + 1) / k as i64;
res[k as usize] = curr as i32;
prev = curr;
}
res
}
}
No developer has claimed the Kotlin throne for LeetCode #119 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
Each element in a row of Pascal's triangle can be calculated from the previous element in the same row using combinatorics.
The k-th element of the n-th row is given by the combination formula C(n, k).
We can compute C(n, k) based on C(n, k-1) by multiplying it by (n - k + 1) and then dividing by k.
This allows us to generate the entire row in a single pass without needing to compute previous rows.
class Solution {
fun getRow(rowIndex: Int): List<Int> {
// Initialize array with 1s
val res = IntArray(rowIndex + 1) { 1 }
var prev: Long = 1L
for (k in 1 until rowIndex) {
// Use Long to prevent overflow during intermediate multiplication
val curr = prev * (rowIndex - k + 1) / k
res[k] = curr.toInt()
prev = curr
}
return res.toList()
}
}
No developer has claimed the Swift throne for LeetCode #119 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
Each element in a row of Pascal's triangle can be calculated from the previous element in the same row using combinatorics.
The k-th element of the n-th row is given by the combination formula C(n, k).
We can compute C(n, k) based on C(n, k-1) by multiplying it by (n - k + 1) and then dividing by k.
This allows us to generate the entire row in a single pass without needing to compute previous rows.
class Solution {
func getRow(_ rowIndex: Int) -> [Int] {
// Initialize array of given size with 1s
var res = Array(repeating: 1, count: rowIndex + 1)
var prev = 1
if rowIndex > 1 {
for k in 1..<rowIndex {
// Compute using combination formula
// Standard Int in Swift is 64-bit on modern Apple platforms, avoiding overflow for rowIndex <= 33
let curr = prev * (rowIndex - k + 1) / k
res[k] = curr
prev = curr
}
}
return res
}
}
No developer has claimed the C++ throne for LeetCode #119 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
Each element in a row of Pascal's triangle can be calculated from the previous element in the same row using combinatorics.
The k-th element of the n-th row is given by the combination formula C(n, k).
We can compute C(n, k) based on C(n, k-1) by multiplying it by (n - k + 1) and then dividing by k.
This allows us to generate the entire row in a single pass without needing to compute previous rows.
class Solution {
public:
vector<int> getRow(int rowIndex) {
// Initialize vector with size rowIndex + 1 and default value 1
vector<int> res(rowIndex + 1, 1);
long long prev = 1;
// Iterate through the inner elements
for (int k = 1; k < rowIndex; ++k) {
// Use long long to avoid overflow when multiplying large numbers
long long curr = prev * (rowIndex - k + 1) / k;
res[k] = curr;
prev = curr;
}
return res;
}
};
No developer has claimed the C# throne for LeetCode #119 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
Each element in a row of Pascal's triangle can be calculated from the previous element in the same row using combinatorics.
The k-th element of the n-th row is given by the combination formula C(n, k).
We can compute C(n, k) based on C(n, k-1) by multiplying it by (n - k + 1) and then dividing by k.
This allows us to generate the entire row in a single pass without needing to compute previous rows.
public class Solution {
public IList<int> GetRow(int rowIndex) {
int[] res = new int[rowIndex + 1];
// Set the first element
res[0] = 1;
long prev = 1;
for (int k = 1; k <= rowIndex; k++) {
// Compute the next element using the combination formula
// We use 'long' to prevent multiplication overflow
long curr = prev * (rowIndex - k + 1) / k;
res[k] = (int)curr;
prev = curr;
}
return res;
}
}