200ms Java simple loop solution


  • 3
    T
    public class Solution {
        public List<List<Integer>> generate(int numRows) {
            if(numRows == 0) return new ArrayList<>();
            List<List<Integer>> result = new ArrayList<>();
            List<Integer> first = new ArrayList<>();
            first.add(1);
            result.add(first);
            List<Integer> lastLevel;
            List<Integer> currentLevel;
            for(int i = 1; i < numRows; i++) {
                lastLevel = result.get(i-1);
                currentLevel = new ArrayList<>();
                currentLevel.add(1);
                for(int j = 1; j < lastLevel.size(); j++) {
                    currentLevel.add(lastLevel.get(j-1) + lastLevel.get(j));
                }
                currentLevel.add(1);
                result.add(currentLevel);
            }
            return result;
        }
    }

Log in to reply
 

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