My java solution


  • 0
    C
    public class Solution {
    public List<List<Integer>> generate(int numRows) {
    List<List<Integer>> lists = new ArrayList();
    if(numRows == 0)return lists;
    //base case
    List<Integer> list1 = new ArrayList<Integer>();
    list1.add(1);
    lists.add(list1);
    if(numRows == 1) return lists;
    
    ArrayList<Integer> list2 = new ArrayList<Integer>();
    list2.add(1);list2.add(1);
    lists.add(list2);
    if(numRows == 2)return lists;
    
    for(int i = 2;i<numRows;i++)
    {
    	ArrayList<Integer> list = new ArrayList<Integer>();
    	list.add(0, 1);
    	List<Integer> prelist = lists.get(i-1);
    	for(int j=1;j<i;j++)
    	{	
    		int num = prelist.get(j-1)+prelist.get(j);
    		list.add(num);
    	}	
    	list.add(i, 1);
    	lists.add(list);
    }
    return lists;
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.