• 5.数据结构--递归


    将原来的问题,转化为更小的同一问题

    public class Sum {
        public static int sum(int[] arr){
            return sum(arr,0);
        }
    
        //计算arr[l...n]这个区间内所有数字的和
        private static int sum(int[] arr,int l){
            //求解最基本的问题
            if(l == arr.length)
                return 0;
    
            //把原问题转化为更小的问题
            return arr[l] + sum(arr,l + 1);
        }
    
        public static void main(String[] args) {
            int[] nums = {1, 2, 3, 4, 5, 6, 7, 8};
            System.out.println(sum(nums)); //36
        }
    }

    1.链表的天然递归结构性质

    解决链表中删除元素的问题

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    class Solution3 {
        public ListNode removeElements(ListNode head, int val) {
           if(head == null)
               return null;
    
           head.next = removeElements(head.next,val);
    
           return head.val == val ? head.next : head;
        }
    
        public static void main(String[] args) {
            int[] nums = {1, 2, 6, 3, 4, 5, 6};
            ListNode head = new ListNode(nums);
            System.out.println(head);  //1->2->6->3->4->5->6->NULL
    
            ListNode res = (new Solution()).removeElements(head , 6);
            System.out.println(res);   //1->2->3->4->5->NULL
        }
    }

    2.递归运行的机制

     

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    class Solution4 {
        public ListNode removeElements(ListNode head, int val,int depth) {
            String depthString = generateDepthString(depth);
            System.out.print(depthString);
            System.out.println("Call: remove " + val +" "+ head);
            if(head == null){
                System.out.print(depthString);
                System.out.println("Return " + head);
                return null;
            }
    
            ListNode res = removeElements(head.next,val,depth + 1);
            System.out.print(depthString);
            System.out.println("After remove " + val + ": " + res);
    
            ListNode ret;
            if(head.val == val)
                ret = res;
            else{
                head.next = res;
                ret = head;
            }
            System.out.print(depthString);
            System.out.println("Return: " + ret);
            return ret;
        }
    
        private String generateDepthString(int depth){
            StringBuilder res = new StringBuilder();
            for(int i = 0;i < depth;i ++){
                res.append("--");
            }
            return res.toString();
        }
    
        public static void main(String[] args) {
            int[] nums = {1, 2, 6, 3, 4, 5, 6};
            ListNode head = new ListNode(nums);
            System.out.println(head);
    
            ListNode res = (new Solution4()).removeElements(head , 6,0);
            System.out.println(res);
    //        1->2->6->3->4->5->6->NULL
    //        Call: remove 6 1->2->6->3->4->5->6->NULL
    //                --Call: remove 6 2->6->3->4->5->6->NULL
    //                ----Call: remove 6 6->3->4->5->6->NULL
    //                ------Call: remove 6 3->4->5->6->NULL
    //                --------Call: remove 6 4->5->6->NULL
    //                ----------Call: remove 6 5->6->NULL
    //                ------------Call: remove 6 6->NULL
    //                --------------Call: remove 6 null
    //                --------------Return null
    //                ------------After remove 6: null
    //                ------------Return: null
    //                ----------After remove 6: null
    //                ----------Return: 5->NULL
    //                --------After remove 6: 5->NULL
    //                --------Return: 4->5->NULL
    //                ------After remove 6: 4->5->NULL
    //                ------Return: 3->4->5->NULL
    //                ----After remove 6: 3->4->5->NULL
    //                ----Return: 3->4->5->NULL
    //                --After remove 6: 3->4->5->NULL
    //                --Return: 2->3->4->5->NULL
    //        After remove 6: 2->3->4->5->NULL
    //        Return: 1->2->3->4->5->NULL
    //        1->2->3->4->5->NULL
        }
    }
  • 相关阅读:
    HDU 4320 Arcane Numbers 1(质因子包含)
    BZOJ 3673: 可持久化并查集(可持久化并查集+启发式合并)
    Codeforces Beta Round #65 (Div. 2) C. Round Table Knights
    HDU 4496 D-City(逆向并查集)
    HDU 3047 Zjnu Stadium(带权并查集)
    HDU 4104 Discount(n个数不能构成的最小值)
    hihoCoder 1515 分数调查(带权并查集)
    POJ 1733 Parity game(种类并查集)
    Codeforces Round #107 (Div. 1) B. Quantity of Strings(推算)
    CSU 2005 Nearest Maintenance Point(最短路+bitset)
  • 原文地址:https://www.cnblogs.com/zouke1220/p/9501676.html
Copyright © 2020-2023  润新知