Accepted Java solution using recursion


  • 1
    S
    public class Solution {
        public List<Integer> getRow(int rowIndex) {
            if (rowIndex==0) {
                List<Integer> r = new ArrayList<Integer>();
                r.add(1);
                return r;
            } 
            if (rowIndex==1) {
                List<Integer> r = new ArrayList<Integer>();
                r.add(1);
                r.add(1);
                return r;
            }
            List<Integer> oldRow = getRow(rowIndex-1);
            List<Integer> newRow = new ArrayList<Integer>();
            newRow.add(1);
            for(int i=0;i<oldRow.size()-1;i++) {
                newRow.add(oldRow.get(i)+oldRow.get(i+1));
            }
            newRow.add(1);
            return newRow;
            
        }
    }

Log in to reply
 

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