Recursive solution Java. 3ms


  • 1
    L
        public List<Integer> getRow(int rowIndex) {
            if (rowIndex == 0) return Arrays.asList(1);
            List<Integer> previousList = getRow(rowIndex - 1);
            List<Integer> thisList = new ArrayList<>();
            for (int i =0; i < previousList.size(); i++) {
                if (i == 0) thisList.add(1);
                if (i > 0) thisList.add(previousList.get(i) + previousList.get(i-1));
                if (i == previousList.size() -1) thisList.add(1);
            }
            return thisList;
        }
    

  • 0
    S

    Elegant. Thanks a lot.


Log in to reply
 

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