A solution in Java


  • 0
    Y

    import java.util.ArrayList;

    public class Solution {
    public ArrayList<ArrayList<Integer>> generate(int numRows) {
    ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();

            if (numRows<=0) return result;
            
            result.add (new ArrayList());
            result.get(0).add(1);
            if (numRows==1) return result;
            
            result.add (new ArrayList());
            result.get(1).add(1);
            result.get(1).add(1);
            if (numRows==2) return result;
            
        for (int i=2; i<numRows;i++ ){
            result.add (new ArrayList());
            result.get(i).add(1);
            int j=0;
            while (j<result.get(i-1).size()-1){
                
                result.get(i).add(result.get(i-1).get(j) + result.get(i-1).get(j+1));
                j++;
            }
            result.get(i).add(1);
        }
        
        return result;
        
        
        
        
    }
    

    }


Log in to reply
 

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