Skip to content

Instantly share code, notes, and snippets.

@thinkphp
Created March 29, 2026 17:40
Show Gist options
  • Select an option

  • Save thinkphp/d11ca1237a94e387a9d8397a6cd4c3a7 to your computer and use it in GitHub Desktop.

Select an option

Save thinkphp/d11ca1237a94e387a9d8397a6cd4c3a7 to your computer and use it in GitHub Desktop.
Triangle-Pascal.java
/*
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