菜鸟刷的第一题LeetCode,毫无任何优化,虽然是一道easy的题目,看到Accepted心里还是甚是欣慰的。
1 import java.util.ArrayList; 2 import java.util.List; 3 import java.util.Scanner; 4 public class Solution { 5 public List<List<Integer>> generate(int numRows) { 6 List<List<Integer>> pascal = new ArrayList<List<Integer>>(); 7 for (int i = 0; i < numRows; i++) { 8 List<Integer> list = new ArrayList<Integer>(); 9 for (int j = 0; j < i+1; j++) { 10 if(i==0 ||i == 1) 11 { 12 list.add(1); 13 } 14 else 15 { 16 if(j==0||j==i) 17 { 18 list.add(1); 19 } 20 else 21 { 22 list.add(pascal.get(i-1).get(j-1)+pascal.get(i-1).get(j)); 23 } 24 } 25 } 26 pascal.add(list); 27 //list.clear(); 28 } 29 return pascal; 30 31 } 32 public static void main(String args[]) 33 { 34 Solution sl = new Solution(); 35 Scanner sc = new Scanner(System.in); 36 int n = sc.nextInt(); 37 List<List<Integer>> pascal = sl.generate(n); 38 for(int i = 0;i<n;i++) 39 { 40 System.out.println(pascal.get(i)); 41 } 42 } 43 }