Share my java 2 ms solution...


  • 0
    J

    public static List<List<Integer>> generate(int numRows){
    List<List<Integer>>list=new ArrayList<>();
    if(numRows<1) return list;
    if(numRows==1){
    List<Integer> arr=new ArrayList<>();
    arr.add(1);
    list.add(arr);
    return list;

    }
    for(int i=0;i<numRows;i++){
    	list.add(Arrays.asList(new Integer[i+1]));
    }
    list.get(0).set(0, 1);
    list.get(1).set(0, 1);
    list.get(1).set(1, 1);
    for(int i=2;i<list.size();i++){
    	for(int j=0;j<list.get(i).size();j++){
    		if(j==0||j==list.get(i).size()-1){
    			list.get(i).set(j, 1);
    		}else{
    			list.get(i).set(j, list.get(i-1).get(j-1)+list.get(i-1).get(j));
    		}
    	}
    }
    return list;
    

    }


Log in to reply
 

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