📜  在帕斯卡三角形中找到第N行

📅  最后修改于: 2021-06-25 19:58:38             🧑  作者: Mango

给定一个非负整数N ,任务是找到帕斯卡三角形的N行

注意:行索引从0开始。

例子:

天真的方法:
解决问题的最简单方法是使用递归。首先使用递归查找上一个索引的行,然后在上一个索引的帮助下计算当前行的值。重复此过程,直至N列。

下面是上述方法的实现:

C++
// C++ program to find the Nth
// index row in Pascal's triangle
#include 
using namespace std;
 
// Function to find the elements
// of rowIndex in Pascal's Triangle
vector getRow(int rowIndex)
{
    vector currow;
     
    // 1st element of every row is 1
    currow.push_back(1);
     
    // Check if the row that has to
    // be returned is the first row
    if (rowIndex == 0)
    {
        return currow;
    }
     
    // Generate the previous row
    vector prev = getRow(rowIndex - 1);
 
    for(int i = 1; i < prev.size(); i++)
    {
         
        // Generate the elements
        // of the current row
        // by the help of the
        // previous row
        int curr = prev[i - 1] + prev[i];
        currow.push_back(curr);
    }
    currow.push_back(1);
     
    // Return the row
    return currow;
}
 
// Driver Code   
int main()
{
    int n = 3;
    vector arr = getRow(n);
 
    for(int i = 0; i < arr.size(); i++)
    {
        if (i == arr.size() - 1)
            cout << arr[i];
        else
            cout << arr[i] << ", ";
    }
    return 0;
}
 
// This code is contributed by divyesh072019


Java
// Java Program to find the Nth
// index row in Pascal's triangle
import java.util.ArrayList;
public class geeks {
 
    // Function to find the elements
    // of rowIndex in Pascal's Triangle
    public static ArrayList getRow(
        int rowIndex)
    {
        ArrayList currow
            = new ArrayList();
        // 1st element of every row is 1
        currow.add(1);
 
        // Check if the row that has to
        // be returned is the first row
        if (rowIndex == 0) {
            return currow;
        }
        // Generate the previous row
        ArrayList prev = getRow(rowIndex
                                         - 1);
 
        for (int i = 1; i < prev.size(); i++) {
            // Generate the elements
            // of the current row
            // by the help of the
            // previous row
            int curr = prev.get(i - 1)
                       + prev.get(i);
            currow.add(curr);
        }
        currow.add(1);
 
        // Return the row
        return currow;
    }
 
    // Driver Program
    public static void main(String[] args)
    {
        int n = 3;
        ArrayList arr = getRow(n);
 
        for (int i = 0; i < arr.size(); i++) {
            if (i == arr.size() - 1)
                System.out.print(arr.get(i));
            else
                System.out.print(arr.get(i)
                                 + ", ");
        }
    }
}


Python3
# Python3 program to find the Nth
# index row in Pascal's triangle
 
# Function to find the elements
# of rowIndex in Pascal's Triangle
def getRow(rowIndex) :
 
    currow = []
     
    # 1st element of every row is 1
    currow.append(1)
     
    # Check if the row that has to
    # be returned is the first row
    if (rowIndex == 0) :
     
        return currow
     
    # Generate the previous row
    prev = getRow(rowIndex - 1)
 
    for i in range(1, len(prev)) :
         
        # Generate the elements
        # of the current row
        # by the help of the
        # previous row
        curr = prev[i - 1] + prev[i]
        currow.append(curr)
 
    currow.append(1)
     
    # Return the row
    return currow
 
n = 3
arr = getRow(n)
 
for i in range(len(arr)) :
 
    if (i == (len(arr) - 1)) :
        print(arr[i])
    else :
        print(arr[i] , end = ", ")
 
# This code is contributed by divyeshrabadiya07


C#
// C# program to find the Nth
// index row in Pascal's triangle
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to find the elements
// of rowIndex in Pascal's Triangle
public static List getRow(int rowIndex)
{
    List currow = new List();
     
    // 1st element of every row is 1
    currow.Add(1);
 
    // Check if the row that has to
    // be returned is the first row
    if (rowIndex == 0)
    {
        return currow;
    }
    // Generate the previous row
    List prev = getRow(rowIndex - 1);
 
    for(int i = 1; i < prev.Count; i++)
    {
         
        // Generate the elements
        // of the current row
        // by the help of the
        // previous row
        int curr = prev[i - 1] + prev[i];
        currow.Add(curr);
    }
    currow.Add(1);
 
    // Return the row
    return currow;
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 3;
    List arr = getRow(n);
 
    for(int i = 0; i < arr.Count; i++)
    {
        if (i == arr.Count - 1)
            Console.Write(arr[i]);
        else
            Console.Write(arr[i] + ", ");
    }
}
}
 
// This code is contributed by 29AjayKumar


Javascript


C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Print the N-th row of the
// Pascal's Triangle
void generateNthrow(int N)
{
    // nC0 = 1
    int prev = 1;
    cout << prev;
 
    for (int i = 1; i <= N; i++) {
        // nCr = (nCr-1 * (n - r + 1))/r
        int curr = (prev * (N - i + 1)) / i;
        cout << ", " << curr;
        prev = curr;
    }
}
 
// Driver Program
int main()
{
 
    int N = 5;
    generateNthrow(N);
    return 0;
}


Java
// Java program to implement the above approach
import java.io.*;
 
class GFG{
 
// Print the N-th row of the
// Pascal's Triangle
static void generateNthrow(int N)
{
 
    // nC0 = 1
    int prev = 1;
    System.out.print(prev);
     
    for(int i = 1; i <= N; i++)
    {
         
       // nCr = (nCr-1 * (n - r + 1))/r
       int curr = (prev * (N - i + 1)) / i;
       System.out.print(", " + curr);
       prev = curr;
    }
}
     
// Driver code
public static void main (String[] args)
{
    int N = 5;
    generateNthrow(N);
}
}
 
// This code is contributed by shubhamsingh10


Python3
# Python3 program to implement the above approach
 
# Print the N-th row of the
# Pascal's Triangle
def generateNthRow (N):
 
    # nC0 = 1
    prev = 1
    print(prev, end = '')
 
    for i in range(1, N + 1):
 
        # nCr = (nCr-1 * (n - r + 1))/r
        curr = (prev * (N - i + 1)) // i
        print(",", curr, end = '')
        prev = curr
 
# Driver code
N = 5
 
# Function calling
generateNthRow(N)
 
# This code is contributed by himanshu77


C#
// C# program to implement the above approach
using System;
using System.Collections.Generic;
class GFG{
 
// Print the N-th row of the
// Pascal's Triangle
static void generateNthrow(int N)
{
 
    // nC0 = 1
    int prev = 1;
    Console.Write(prev);
     
    for(int i = 1; i <= N; i++)
    {
         
        // nCr = (nCr-1 * (n - r + 1))/r
        int curr = (prev * (N - i + 1)) / i;
        Console.Write(", " + curr);
        prev = curr;
    }
}
     
// Driver code
public static void Main(String[] args)
{
    int N = 5;
    generateNthrow(N);
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
1, 3, 3, 1

高效方法:
请按照以下步骤优化上述方法:

  • 不同于上述方法,我们将只产生唯一的N个的数量第i行。
  • 我们可以观察到帕斯卡三角形的N行由以下序列组成:
NC0, NC1, ......, NCN - 1, NCN
  • 由于N C 0 = 1 ,因此可以通过以下等式生成序列的以下值:
NCr = (NCr - 1 * (N - r + 1)) / r where 1 ≤ r ≤ N

下面是上述方法的实现:

C++

// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Print the N-th row of the
// Pascal's Triangle
void generateNthrow(int N)
{
    // nC0 = 1
    int prev = 1;
    cout << prev;
 
    for (int i = 1; i <= N; i++) {
        // nCr = (nCr-1 * (n - r + 1))/r
        int curr = (prev * (N - i + 1)) / i;
        cout << ", " << curr;
        prev = curr;
    }
}
 
// Driver Program
int main()
{
 
    int N = 5;
    generateNthrow(N);
    return 0;
}

Java

// Java program to implement the above approach
import java.io.*;
 
class GFG{
 
// Print the N-th row of the
// Pascal's Triangle
static void generateNthrow(int N)
{
 
    // nC0 = 1
    int prev = 1;
    System.out.print(prev);
     
    for(int i = 1; i <= N; i++)
    {
         
       // nCr = (nCr-1 * (n - r + 1))/r
       int curr = (prev * (N - i + 1)) / i;
       System.out.print(", " + curr);
       prev = curr;
    }
}
     
// Driver code
public static void main (String[] args)
{
    int N = 5;
    generateNthrow(N);
}
}
 
// This code is contributed by shubhamsingh10

Python3

# Python3 program to implement the above approach
 
# Print the N-th row of the
# Pascal's Triangle
def generateNthRow (N):
 
    # nC0 = 1
    prev = 1
    print(prev, end = '')
 
    for i in range(1, N + 1):
 
        # nCr = (nCr-1 * (n - r + 1))/r
        curr = (prev * (N - i + 1)) // i
        print(",", curr, end = '')
        prev = curr
 
# Driver code
N = 5
 
# Function calling
generateNthRow(N)
 
# This code is contributed by himanshu77

C#

// C# program to implement the above approach
using System;
using System.Collections.Generic;
class GFG{
 
// Print the N-th row of the
// Pascal's Triangle
static void generateNthrow(int N)
{
 
    // nC0 = 1
    int prev = 1;
    Console.Write(prev);
     
    for(int i = 1; i <= N; i++)
    {
         
        // nCr = (nCr-1 * (n - r + 1))/r
        int curr = (prev * (N - i + 1)) / i;
        Console.Write(", " + curr);
        prev = curr;
    }
}
     
// Driver code
public static void Main(String[] args)
{
    int N = 5;
    generateNthrow(N);
}
}
 
// This code is contributed by 29AjayKumar

Java脚本


输出:
1, 5, 10, 10, 5, 1

时间复杂度: O(N)
辅助空间: O(1)

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。