Skip to content

jayjieh/PascalTriangle

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Pascal's Triangle

Given an index k, return the kth row of the Pascal's triangle

Example:

For example, when k = 3, the row is [1,3,3,1]

Prerequisite

  • Ensure you have Java 8 installed ,

To get the the kth row we do :

    1. Initialize the result
           ArrayList<Integer> result = new ArrayList<>();
    1. Validate to ensure no kth value less than zero is given
         if (rowIndex < 0) {
              return result;
           }
    1. We then add the next element
         result.add(1);
    1. We then loop through against the number of rows to fnd the row listing the kth

      for (int i = 1; i <= rowIndex; i++) {
          for (int j = result.size() - 2; j >= 0; j--) {
              result.set(j + 1, result.get(j) + result.get(j + 1));
          }
          result.add(1);
      }

License

Owori Juma

About

Given an index k, return the kth row of the Pascal's triangle

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages