LeetCode Brushing Questions: Recursion

The difference between recursion and backtracking:
recursion is able to determine the unique solution, and go deeper in one direction.
Backtracking is a different decision. Each step requires trial, and it needs to decide whether to roll back according to the state.

example

439. Ternary Expression Parser

https://leetcode-cn.com/problems/ternary-expression-parser/
Insert picture description here

Key point: If
T, take 3-the last: the data between
F, take the last: the data after
Insert picture description here

21. Merge two ordered linked lists

https://leetcode-cn.com/problems/merge-two-sorted-lists/

Solution 1:
Java comes with API, sort

Practice point:
the construction and return of the linked list

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    
    
    private ListNode buildFromList(List<Integer> list) {
    
    
        ListNode result = new ListNode();
        ListNode tmp = result;
        for (int i = 0; i < list.size(); i++) {
    
    
            ListNode next = null;
            if(i != list.size() - 1) {
    
    
                next = new ListNode();
            }            
            tmp.val = list.get(i);
            tmp.next = next;
            tmp = next;
        }
        return result;
    }
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
    
    
        List<Integer> list = new LinkedList<>();
        if (l1 == null) {
    
    
            return l2;
        }
        if (l2 == null) {
    
    
            return l1;
        }
        while (l1.next != null) {
    
    
            list.add(l1.val);
            l1 = l1.next;
        }
        list.add(l1.val);
        while (l2.next != null) {
    
    
            list.add(l2.val);
            l2 = l2.next;
        }
        list.add(l2.val);
        if (list.isEmpty()) {
    
    
            return null;
        }
        list.sort(Comparator.naturalOrder());
        return buildFromList(list);
    }
}

Solution 2: Recursion

class Solution {
    
    
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
    
    
   // 设置出口条件1:异常出口条件
        if (l1 == null) {
    
    
            return l2;
        }
        if (l2 == null) {
    
    
            return l1;
        }
   
   // 正常业务逻辑
        ListNode result = null;
        if(l2.val > l1.val) {
    
    
  // 本次处理部分
            result = l1;
  // 下次处理部分
            result.next = mergeTwoLists(l1.next, l2);
        } else {
    
    
            result = l2;
            result.next = mergeTwoLists(l1, l2.next);
        }
 
   // 设置出口条件2:正常出口条件
        return result;
    }
}

779. The Kth Grammar Symbol

Insert picture description here
Violent solution: The
Insert picture description hereproblem is: timeout.
Reason: The next string will be calculated every recursion. The problem caused by this is that the multiple of the string 2 grows in length, and it cannot be processed when N is relatively large.

Solution: only need to get the solution without string replacement
Insert picture description here

It is easy to understand that the Kth number of each line is generated from the previous time, and we only need to record one digit instead of repeatedly finding the string each time.

Guess you like

Origin blog.csdn.net/weixin_38370441/article/details/115252451