Created
March 29, 2026 17:40
-
-
Save thinkphp/d11ca1237a94e387a9d8397a6cd4c3a7 to your computer and use it in GitHub Desktop.
Triangle-Pascal.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| 1 | |
| 1 1 | |
| 1 2 1 | |
| 1 3 3 1 | |
| 1 4 6 4 1 | |
| 1 | |
| 1 1 | |
| 1 1 1 | |
| 1 1 1 1 | |
| */ | |
| import java.util.*; | |
| public class Solution { | |
| public List<List<Integer>> generate(int numRows) { | |
| List<List<Integer>> v = new ArrayList<>(); | |
| for(int i = 0; i < numRows; ++i) { | |
| List<Integer> v1 = new ArrayList<>(); | |
| for(int j = 0; j <= i; ++j) { | |
| if(j == 0 || j == i) { | |
| v1.add( 1 ); | |
| } else { | |
| int t = v.get(i - 1).get(j - 1) + v.get(i - 1).get(j); | |
| v1.add( t ); | |
| } | |
| } | |
| v.add(v1); | |
| } | |
| return v; | |
| } | |
| public static void main(String[] args) { | |
| Scanner sc = new Scanner(System.in); | |
| Solution sol = new Solution(); | |
| System.out.print("Introduceti numarul de randuri: "); | |
| int n = sc.nextInt(); | |
| List<List<Integer>> triangle = sol.generate( n ); | |
| System.out.println("Pascal Triangle:"); | |
| for(List<Integer> row: triangle) { | |
| for(int val: row) { | |
| System.out.print(val + " "); | |
| } | |
| System.out.println(); | |
| } | |
| } | |
| } | |
| /* | |
| 1 2 3 4 (linia 1) | |
| Y Z 3 4 (linia 2)(coloana 1) | |
| 1 X 3 4 (linia 3)(coloana 2) | |
| 1 2 3 4 (linia 4) | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment