Share my accepted java solution


  • 0
    H

    Quite straightforward and easy to understand.

    public List<List<Integer>> generate(int numRows) {
            List<List<Integer>> re = new ArrayList<List<Integer>>();
    		if(numRows < 1){
    			return re;
    		}
    		
    		for(int i = 1; i <= numRows; i++){
    			List<Integer> list = new ArrayList<Integer>();
    			list.add(1);
    			if(i >= 3){
    				for(int j = 1; j < i - 1; j++){
    					list.add(j, re.get(i-2).get(j - 1) + re.get(i-2).get(j));
    				}
    			}
    			if(i > 1){
    				list.add(1);
    			}
    			
    			re.add(list);
    		}
    		return re;
    	}

Log in to reply
 

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