• 专题一:递归【递归、搜索、回溯】


    什么是递归

    函数自己调用自己的情况。

    为什么要用递归

    主问题->子问题        子问题->子问题

    宏观看待递归

    不要在意细节展开图,把函数当成一个黑盒,相信这个黑盒一定能完成任务。

     如何写好递归

    一、汉诺塔

     

    1. class Solution {
    2. public:
    3. void dfs(vector<int>& A,vector<int>& B,vector<int>& C,int n)
    4. {
    5. if(n == 1)
    6. {
    7. C.push_back(A.back());
    8. A.pop_back();
    9. return;
    10. };
    11. dfs(A,C,B,n-1);
    12. C.push_back(A.back());
    13. A.pop_back();
    14. dfs(B,A,C,n-1);
    15. }
    16. void hanota(vector<int>& A, vector<int>& B, vector<int>& C) {
    17. dfs(A,B,C,A.size());
    18. }
    19. };

     二、合并两个有序链表

    1. /**
    2. * Definition for singly-linked list.
    3. * struct ListNode {
    4. * int val;
    5. * ListNode *next;
    6. * ListNode() : val(0), next(nullptr) {}
    7. * ListNode(int x) : val(x), next(nullptr) {}
    8. * ListNode(int x, ListNode *next) : val(x), next(next) {}
    9. * };
    10. */
    11. class Solution {
    12. public:
    13. ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
    14. if(l1 == nullptr)return l2;
    15. if(l2 == nullptr) return l1;
    16. if(l1->val <= l2->val)
    17. {
    18. l1->next = mergeTwoLists(l1->next,l2);
    19. return l1;
    20. }
    21. else
    22. {
    23. l2->next = mergeTwoLists(l1,l2->next);
    24. return l2;
    25. }
    26. }
    27. };

     

    三、反转链表 

    1. class Solution {
    2. public:
    3. ListNode* reverseList(ListNode* head) {
    4. if(head == nullptr || head->next == nullptr) return head;
    5. ListNode* newhead = reverseList(head->next);
    6. head->next->next = head;
    7. head->next = nullptr;
    8. return newhead;
    9. }
    10. };

     四、两两交换链表中的结点

    分析跟上一题差不多,不详解。

    1. /**
    2. * Definition for singly-linked list.
    3. * struct ListNode {
    4. * int val;
    5. * ListNode *next;
    6. * ListNode() : val(0), next(nullptr) {}
    7. * ListNode(int x) : val(x), next(nullptr) {}
    8. * ListNode(int x, ListNode *next) : val(x), next(next) {}
    9. * };
    10. */
    11. class Solution {
    12. public:
    13. ListNode* swapPairs(ListNode* head) {
    14. if(head == nullptr || head->next == nullptr) return head;
    15. ListNode*newhead = swapPairs(head->next->next);
    16. auto ret = head->next;
    17. head->next->next = head;
    18. head->next = newhead;
    19. return ret;
    20. }

     五、快速幂

    实现 pow(x, n) ,即计算 x 的整数 n 次幂函数(即,x^n )。

     

     

    1. class Solution {
    2. public:
    3. double pow(double x,long long n)
    4. {
    5. if(n == 0)return 1.0;
    6. double tmp = pow(x,n/2);
    7. return n%2 == 0? tmp*tmp:tmp*tmp*x;
    8. }
    9. double myPow(double x, int n) {
    10. if(n < 0) return 1.0/(pow(x,-(long long)n));
    11. return pow(x,n);
    12. }
    13. };

     

  • 相关阅读:
    基于kubenetes的kubespere安装
    记录一个出现多次的小BUG:用串口读的数据,只能有一次赋值
    SpringBoot SpringBoot 开发实用篇 6 监控 6.7 自定义端点
    想转行网络安全,究竟是自学还是参加培训班?
    Android8.1 hal 加载wifi ko模块流程
    linux安装Ibus
    深度学习之生成唐诗案例(Pytorch版)
    简单聊一聊一种很新的DCDC电源-BOB电源
    Shiro中的session和cookie
    CH395实现主动ping对端功能(代码及说明)
  • 原文地址:https://blog.csdn.net/m0_73065213/article/details/133562991