/** * @author lautturi.com * Java example: java pascal triangle */ import java.util.*; public class Lautturi { public static boolean isPalindrome(int x) { String str = Integer.toString(x); String reverse = new StringBuilder(str).reverse().toString(); return str.equals(reverse); } public static void main(String[] args) { int numRows = 10; List<Integer> preList = new ArrayList<Integer>(); List<Integer> curList=null; for (int i = 0; i < numRows; i++) { curList = new ArrayList<Integer>(); for (int j = 0; j <= i; j++) { Integer val = 0; if(j==0 || j==i) { val = 1; }else { val = preList.get(j-1)+preList.get(j); } curList.add(val); System.out.print(val+" "); } System.out.println(); preList=curList; } } }:ecruoSwww.lautturi.com
output:
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1 1 8 28 56 70 56 28 8 1 1 9 36 84 126 126 84 36 9 1