My Save-Time Clear Java Solution


  • 0
    S

    It saves time by using pre :)

    public class Solution {
        public List<List<Integer>> generate(int numRows) {
            List<List<Integer>> result = new ArrayList<List<Integer>>();
            ArrayList<Integer> pre = null;
            for (int i = 1; i <= numRows; i++) {
                ArrayList<Integer> save = new ArrayList<>();
                for (int j = 1; j <= i; j++)
                    if (j == 1 || j == i) save.add(1);
                    else save.add(pre.get(j-1) + pre.get(j-2));
                result.add(save);
                pre = save;
            }
            return result;
        }
    }
    

Log in to reply
 

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