leetcode递归算法题总结

本文详细介绍了递归在编程中的应用,包括汉诺塔问题的解决方案、合并两个有序链表、反转链表以及两两交换链表节点的方法,同时展示了Pow(x,n)的递归实现。
摘要由CSDN通过智能技术生成

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

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;
    }
};
  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值