118. Pascal’s Triangle

118. Pascal’s Triangle

class Solution {
      public static List<List<Integer>> generate(int numRows) {
        List<List<Integer>> all = new ArrayList();
        
      List<Integer> first = new ArrayList<>();
      first.add(1);
      all.add(first);
      
      if(numRows >=2) {
    	  List<Integer> second = new ArrayList<>();
          second.add(1);
          second.add(1);
          all.add(second);  
      }
      for(int i = 2;i < numRows; i++) {
    	  List<Integer> row = new ArrayList<>();
    	  row.add(1);
    	  int start =0;
    	  for(int j = 1; start<all.get(i-1).size()-1; j++) {
    		  int a = all.get(i-1).get(start)+all.get(i-1).get(start+1);
    		  start++;
    		  row.add(a);
    	  }
    	  
    	 row.add(1);
    	 all.add(row); 
      }
        
        return all;
    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s