1 public class Solution { 2 public ArrayList<Integer> getRow(int rowIndex) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 ArrayList<Integer> previous = new ArrayList<Integer>(); 6 ArrayList<Integer> result = new ArrayList<Integer>(); 7 previous.add(1); 8 if(rowIndex == 0) 9 return previous; 10 previous.add(1); 11 if(rowIndex == 1) 12 return previous; 13 for(int i = 1; i < rowIndex; i++) 14 { 15 result.clear(); 16 result.add(1); 17 for(int j = 0; j < previous.size() - 1; j++) 18 { 19 result.add(previous.get(j) + previous.get(j+1)); 20 } 21 result.add(1); 22 previous.clear(); 23 previous.addAll(result); 24 25 } 26 return result; 27 } 28 }
1 public class Solution { 2 public ArrayList<Integer> getRow(int rowIndex) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 ArrayList<Integer> result = new ArrayList<Integer>(); 6 if(rowIndex == 0){ 7 result.add(1); 8 return result; 9 } else if(rowIndex == 1){ 10 result.add(1); 11 result.add(1); 12 return result; 13 } 14 int[] num = new int[rowIndex + 1]; 15 num[0] = 1; 16 num[1] = 1; 17 18 for(int j = 2; j <= rowIndex; j++){ 19 for(int i = j; i >= 0; i--){ 20 if(i == 0 || i == j){ 21 num[i] = 1; 22 } else { 23 num[i] = num[i] + num[i - 1]; 24 } 25 } 26 } 27 for(int i = 0; i <= rowIndex; i++){ 28 result.add(num[i]); 29 } 30 return result; 31 } 32 }