LeetCode Explore: Recursion I

250 阅读2分钟

################################################################

            Num so Far: 13
            with Star * : 1 (review)

################################################################

#24. Swap Nodes in Pairs recursively:

class Solution{
    public ListNode swapPairs(ListNode head){
        if(head == null || head.next == null) return head;
        ListNode nextPair = head.next.next, reversedHead = head.next;
        head.next.next = head;
        head.next = swapPairs(nextPair);
        return reversedHead;
    }
}

#118. Pascal's Triangle

class Solution{
    public List<List<Integer>> generate(int numRows){
        List<List<Integer>> ans = new ArrayList<>();
        if(numRows == 0) return ans;
        return getTri(ans, numRows);
    }
    public List<List<Integer>> getTri(List<List<Integer>> ans, int n){
        List<Integer> curr = new ArrayList<>();
        if(n == 1)
            curr.add(1);
        else{
            List<Integer> last = getTri(ans, n - 1).get(n - 2);
            for(int i = 0; i < n; i++){
                curr.add( i == 0 || i == (n - 1) ? 1 : last.get(i) + last.get(i - 1));
            }
        }
        ans.add(curr);
        return ans;
    }
}

#119. Pascal's Triangle II

class Solution{
    public List<Integer> getRow(int rowIndex){
        List<Integer> ans = new ArrayList<>();
        if(rowIndex == 0) ans.add(1);
        else{
            List<Integer> last = getRow(rowIndex - 1);
            for(int i = 0; i <= rowIndex; i++){
                if(i == 0 || i == rowIndex) ans.add(1);
                else ans.add(last.get(i) + last.get(i - 1));
            }
        }
        return ans;
    }
}

#206. Reverse Linked List

class Solution{
    public ListNode reverseList(ListNode head){
        if(head == null || head.next == null) return head;
        ListNode newHead = reverseList(head.next);
        head.next.next = head;
        head.next = null;
        return newHead;
    }
}

#509. Fibonacci Number

class Solution{
    private Map<Integer,Integer> hm = new HashMap<>();
    private int curr;
    public int fib(int n){
        if(n < 2) curr = n;
        else if(hm.containsKey(n)) curr = hm.get(n);
        else curr = fib(n - 1) + fib(n - 2);
        hm.put(n, curr);
        return curr;
    }
}

#70. Climbing Stairs

class Solution{
    private Map<Integer,Integer> hm = new HashMap<>();
    private int curr;
    public int climbStairs(int n){
        //this is weird since n = 0 returns 1, okay..
        if(n == 0) return 0;//dont put in
        if(n <= 2) curr = n;
        else if(hm.containsKey(n)) curr = hm.get(n);
        else curr = climbStairs(n - 1) + climbStairs(n - 2);
        hm.put(n, curr);
        return curr;
    }
}

#104. Maximum Depth of Binary Tree

class Solution{
    public int maxDepth(TreeNode root){
        if(root == null) return 0;
        return Math.max(maxDepth(root.left), maxDepth(root.right));
    }
}

#50. Pow(x, n)

class Solution{
    public double recurPow(double x, int n){
        if(n == 0) return 1.0;
        if(n == 1) return x;
        double sqrt = recurPow(x, n / 2);
        if(n % 2 == 0) return sqrt * sqrt;
        else return sqrt * sqrt * x;
    }
    public double myPow(double x, int n){
        if(n < 0){
            x = 1.0 / x;
            n *= -1;
        }
        return recurPow(x, n);
    }
}

#21. Merge Two Sorted Lists

class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2){
    ListNode dummyhead = new ListNode(-1), cur = dummyhead;
    merge(cur,l1,l2);
    return dummyhead.next;
    
}
public void merge(ListNode head, ListNode l1, ListNode l2){
    if(l1 == null && l2 == null) return;
    int v1 = (l1 == null) ? Integer.MAX_VALUE : l1.val;
    int v2 = (l2 == null) ? Integer.MAX_VALUE : l2.val;
    if(v1 <= v2){
        head.next = l1;
        l1 = l1.next;
    }
    else{
        head.next = l2;
        l2 = l2.next;
    }
    merge(head.next, l1, l2);
}
}

#779. K-th Symbol in Grammar tail recursion

class Solution{
    public int kthGrammar(int n, int k) {
        return Character.getNumericValue(kth(n, k));
    }
    public char kth(int n, int k){
        if(n == 1) return '0';
        if(k % 2 == 0) return kth(n - 1, k / 2) == '1' ? '0' : '1';
        return kth(n - 2, k / 2 + 1);
    }
}

#95. Unique Binary Search Trees II (*)

class Solution{
    public List<TreeNode> generateTrees(int n) {
        if(n == 0) return new LinkedList<TreeNode>();
        return generate_trees(1,n);
    }
    public LinkedList<TreeNode> generate_trees(int start, int end){
        LinkedList<TreeNode> ans = new LinkedList<>();
        if(start > end){
            ans.add(null);
        }
        for(int i = start; i <= end; i++){
            LinkedList<TreeNode> left_trees = generate_trees(start, i - 1);
            LinkedList<TreeNode> right_trees = generate_trees(i + 1, end);
            for(TreeNode l : left_trees){
                for(TreeNode r : right_trees){
                    TreeNode root = new TreeNode(i);
                    root.left = l;
                    root.right = r;
                    ans.add(root);
                }
            }
        }
        
        return ans;
    }
}