Java recursive solution O(k) space ??


  • 0
    S
    class Solution {
        public List<Integer> getRow(int rowIndex) {
            if(rowIndex < 0) return new ArrayList<>();
            return get(rowIndex);
        }
        
        private List<Integer> get(int n) {
            List<Integer> list = new ArrayList<>();
            list.add(1);
            if(n == 0) return list;
            List<Integer> prev = get(n-1);
            for(int i=1; i<n; i++) {
                list.add(prev.get(i-1) + prev.get(i));
            }
            list.add(1);
            return list;
        }
    }
    

Log in to reply
 

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