leetcode递归算法题总结

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

本章目录

1.汉诺塔

汉诺塔

cpp 复制代码
//面试写法
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);
    }
};
cpp 复制代码
//笔试写法
class Solution {
public:
    void hanota(vector<int>& A, vector<int>& B, vector<int>& C) {
        C =A;
    }
};

2.合并两个有序链表

合并两个有序链表

cpp 复制代码
/**
 * 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.反转链表

反转链表

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

两两交换链表中的节点

cpp 复制代码
/**
 * 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;
    }
};
cpp 复制代码
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)

cpp 复制代码
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;
    }
};
相关推荐
√尖尖角↑2 小时前
力扣——【1991. 找到数组的中间位置】
算法·蓝桥杯
Allen Wurlitzer2 小时前
算法刷题记录——LeetCode篇(1.8) [第71~80题](持续更新)
算法·leetcode·职场和发展
百锦再4 小时前
五种常用的web加密算法
前端·算法·前端框架·web·加密·机密
碳基学AI4 小时前
北京大学DeepSeek内部研讨系列:AI在新媒体运营中的应用与挑战|122页PPT下载方法
大数据·人工智能·python·算法·ai·新媒体运营·产品运营
独家回忆3645 小时前
每日算法-250410
算法
袖清暮雨5 小时前
Python刷题笔记
笔记·python·算法
熬夜造bug6 小时前
LeetCode Hot100 刷题笔记(1)—— 哈希、双指针、滑动窗口
笔记·leetcode·hot100
风掣长空6 小时前
八大排序——c++版
数据结构·算法·排序算法
流星白龙7 小时前
【C++算法】50.分治_归并_翻转对
c++·算法
Java致死8 小时前
费马小定理
算法·费马小定理