Simple solution in Java


  • 0
    H
    It looks simple
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class Solution {
        public List<List<Integer>> generate(int numRows) {
            List<List<Integer>>res = new ArrayList<List<Integer>>();
            if (numRows==0)
                return res;
            List<Integer> tmp = new ArrayList<Integer>();
            tmp.add(1);
            res.add(tmp);
            for (int i = 1; i < numRows; i++) {
                List<Integer>list = new ArrayList<Integer>();
                list.add(1);
                for (int j = 1; j < i; j++) {
                    tmp = res.get(i-1);
                    list.add(tmp.get(j-1)+tmp.get(j));
                }
                list.add(1);
                res.add(list);
            }
            return res;
        }
    }

Log in to reply
 

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