My solution of Java with O(n2) space


  • 0
    C
    public List<List<Integer>> generate(int numRows) {
    		List<List<Integer>> listOfList = new ArrayList<List<Integer>>();
    		List<Integer> list = new ArrayList<Integer>();
    		if(numRows == 0){
    			return listOfList;
    		}
    		list.add(1);
    		listOfList.add(list);
    		
    		if (numRows == 1) {
    			return listOfList;
    		}
    
    		for (int i = 1; i != numRows; i++) {
    			if (i == 1) {
    				list = new ArrayList<Integer>();
    				list.add(1);
    				list.add(1);
    				listOfList.add(list);
    				continue;
    			}
    			list = new ArrayList<Integer>();
    			List<Integer> tempList = listOfList.get(i - 1);
    			for (int j = 0; j != tempList.size(); j++) {
    				if (j == 0) {
    					list.add(tempList.get(j));
    				} else {
    					list.add(tempList.get(j - 1) + tempList.get(j));
    				}
    			}
    			list.add(1);
    			listOfList.add(list);
    		}
    		return listOfList;
    	}

Log in to reply
 

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