leetcode递归算法题总结

2024-01-01 14:12:01

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

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.合并两个有序链表

合并两个有序链表
在这里插入图片描述

/**
 * 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.反转链表

反转链表
在这里插入图片描述

/**
 * 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.两两交换链表中的节点

两两交换链表中的节点
在这里插入图片描述

/**
 * 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)
在这里插入图片描述

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;
    }
};

文章来源:https://blog.csdn.net/m0_55283616/article/details/135317554
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。