admin管理员组

文章数量:1794759

【算法刷题指南】递归

面试题08.06汉诺塔问题

面试题08.06汉诺塔问题

代码语言:javascript代码运行次数:0运行复制
class Solution {
public:
    void dfs(vector<int>& A, vector<int>& B, vector<int>& C,int n)
    {
        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);
    }

    void hanota(vector<int>& A, vector<int>& B, vector<int>& C) {
        dfs(A,B,C,A.size());
    }
};

21.合并两个有序链表

21.合并两个有序链表

代码语言:javascript代码运行次数:0运行复制
/**
 * 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;
        }
    }
};

206.反转链表

206.反转链表

代码语言:javascript代码运行次数:0运行复制
/**
 * 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;
    }
};

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

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

代码语言:javascript代码运行次数:0运行复制
/**
 * 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;
        ListNode* tmp = swapPairs(head->next->next);
        auto ans = head->next;
        head->next->next = head;
        head->next = tmp;
        return ans;
    }
};

快速幂:50.Pow(x,n)

快速幂:50.Pow(x,n)

代码语言:javascript代码运行次数:0运行复制
class Solution {
public:
    double func(double x, long long n) {
        if (n == 0)
            return 1.0;
        double tmp = func(x, n / 2);
        return n % 2 == 0 ? tmp * tmp : tmp * tmp * x;
    }

    double myPow(double x, int n) {
        return n < 0 ? 1.0 / func(x, -(long long)n) : func(x, n);
    }
};
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。 原始发表:2024-10-24,如有侵权请联系 cloudcommunity@tencent 删除intreturn递归链表算法

本文标签: 算法刷题指南递归