leetcode递归算法题总结

递归本质是找重复的子问题

本章目录

  • 1.汉诺塔
  • 2.合并两个有序链表
  • 3.反转链表
  • 4.两两交换链表中的节点
  • 5.Pow(x,n)

1.汉诺塔

汉诺塔
leetcode递归算法题总结_第1张图片

//面试写法
class Solution {
public:
    void hanota(vector<int>& a, vector<int>& b, vector<int>& c) {
        dfs(a,b,c,a.size());
    }
    void dfs(vector<int>& a, vector<int>& b, vector<int>& c,int n)
    {
        if(n==0) return;
        if(n==1)
        {
            c.push_back(a.back());
            a.pop_back();
            return;
        }
        dfs(a,c,b,n-1);
        c.push_back(a.back());
        a.pop_back();
        dfs(b,a,c,n-1);
    }
};
//笔试写法
class Solution {
public:
    void hanota(vector<int>& A, vector<int>& B, vector<int>& C) {
        C =A;
    }
};

2.合并两个有序链表

合并两个有序链表
leetcode递归算法题总结_第2张图片

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
        if(list1 == nullptr) return list2;
        if(list2 == nullptr) return list1;
        if(list1->val<=list2->val) 
        {
            list1->next = mergeTwoLists(list1->next,list2);
            return list1;
        }
        else 
        {
            list2->next = mergeTwoLists(list1,list2->next);
            return list2;
        }
    }
};

3.反转链表

反转链表
leetcode递归算法题总结_第3张图片

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        if(head == nullptr || head->next == nullptr) return head;
        ListNode* newHead = reverseList(head->next);
        head->next->next = head;
        head->next = nullptr;
        return newHead;
    }
};

4.两两交换链表中的节点

两两交换链表中的节点
leetcode递归算法题总结_第4张图片

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        //递归
        if(head == nullptr || head->next == nullptr) return head;
        auto tmp = swapPairs(head->next->next);
        auto ret = head->next;
        head->next->next = head;
        head->next = tmp;
        return ret;
    }
};
class Solution {
public:
    //模拟 循环 迭代
    ListNode* swapPairs(ListNode* head) {
        if(head == nullptr || head->next == nullptr) return head;
        ListNode* ret = new ListNode(0);
        ListNode* prev = ret;
        prev->next = head;
        ListNode* cur = prev->next,*next = cur->next,*nnext = next->next;
        while(cur && next)
        {
            //交换节点
            prev->next = next;
            next->next = cur;
            cur->next = nnext;
            //更新节点
            prev = cur;
            cur = nnext;
            if(cur) next = cur->next;
            if(next) nnext = next->next;
        }
        cur = ret->next;
        delete ret;
        return cur;
    }
};

5.Pow(x,n)

Pow(x,n)
leetcode递归算法题总结_第5张图片

class Solution {
public:
    double myPow(double x, int n) {
        //递归+快速幂
        return n<0? 1.0/pow(x,-(long long)n):pow(x,n);
    }
    double pow(double x,long long n)
    {
        if(n==0) return 1;
        double tmp = pow(x,n/2);
        return n%2==0? tmp*tmp:tmp*tmp*x;
    }
};

你可能感兴趣的:(算法,算法,leetcode,深度优先)