AC Java DP Solution Using Array


  • 0
    W
    public class Solution {
        private List<Integer> buildList(int[] nums, int len){
            List<Integer> res = new ArrayList<>(len);
            for(int i=0;i<len;i++){
                res.add(nums[i]);
            }
            return res;
        }
        public List<List<Integer>> generate(int numRows) {
            List<List<Integer>> res = new ArrayList<>();
            if(numRows==0){
                return res;
            }
            
            int[] array = new int[numRows];
            array[0] = 1;
            res.add(buildList(array, 1));
            
            int len = 2;
            while(len<=numRows){
                for(int i=len-1;i>0;i--){
                    array[i] += array[i-1];
                }
                
                res.add(buildList(array, len));
                len++;
            }
            
            return res;
        }
    }
    

Log in to reply
 

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