算法专题[递归-搜索-回溯-1]

一.汉诺塔

在这里插入图片描述
汉诺塔

1.思路一:

在这里插入图片描述

class Solution {
   
public:
    void hanota(vector<int>& A, vector<int>& B, vector<int>& C) {
   
        int n = A.size();
        def(A,B,C,n);
    }
    void def(vector<int>& A, vector<int>& B, vector<int>& C, int n)
    {
   
        //1.只有一个块的时候:
        if(n==1)
        {
   
            C.push_back(A.back());
            A.pop_back();
            return;
        }
        //2.两块以上
        def(A,C,B,n-1);
        C.push_back(A.back());
        A.pop_back();
        def(B,A,C,n-1);
    }
};

2.GIF题目解析

二.合并两个有序链表

在这里插入图片描述

合并两个有序链表

在这里插入图片描述

1.思路一:

//2.合并两个有序链表:

//2-1:冗余版本

//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_2_1 {
   
 public:
     ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
   
         //1.特殊情况有一个链表为空返回另一个!
         if (list1 == nullptr)
             return list2;
         if (list2 == nullptr)
             return list1;
         //2.给一个头节点进入递归去进行连接:
         ListNode* head = new ListNode();
         ListNode* cur = head;
         //3.递归
         recursion(list1, list2, cur);
         //4.返回
         return head->next;
     }
     void recursion(ListNode*& list1, ListNode*& list2, ListNode*& cur)
     {
   
         //1.递归返回条件
         if (list1 == nullptr || list2 == nullptr)
         {
   
             if (list1 == nullptr && list2 != nullptr)
                 cur->next = list2;
             else if (list1 != nullptr && list2 == nullptr)
                 cur->next = list1;
             return;
         }
         //2.递归移动:
         int val1 = list1->val;
         int val2 = list2->val;

         if (val1 >= val2)
         {
   
             cur->next = list2;
             cur = cur->next;
             ListNode* next2 = cur->next;
             cur->next = nullptr;

             recursion(list1, next2, cur);
         }
         else
         {
   
             cur->next = list1;
             cur = cur->next;
             ListNode* next1 = cur->next;
             cur->next = nullptr;

             recursion(next1, list2, cur);
         }

     }
 };


 //2-2:优化版本

 class Solution {
   
 public:
     ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
   
         //1.特殊情况有一个链表为空返回另一个!
         if (list1 == nullptr)
             return list2;
         if (list2 == nullptr)
             return list1;

         //2.选择一个作为返回的!
         int val1 = list1->val;
         int val2 = list2->val;

         if (val1 >= val2)
         {
   
             list2->next = mergeTwoLists(list1, list2->next);
             return list2;

         }
         else
         {
   
             list1->next = mergeTwoLists(list1->next, list2);
             return list1;
         }

         return nullptr;
     }
 };

2.GIF题目解析

在这里插入图片描述

三.反转链表

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

1.思路一:

在这里插入图片描述


 //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) {
   }
  };
 
  //3-1:复杂版本

  class Solution_3 {
   
  public:
      ListNode* reverseList(ListNode* head) {
   
          if (head == nullptr)
              return nullptr;

          ListNode* newhead = nullptr;
          dfs(head, newhead);
          return newhead;
      }
      ListNode*& dfs(ListNode*& head, ListNode*& rehead)
      {
   
          if (head->next == nullptr)
          {
   
              rehead = head;
              return head;
          }

          ListNode* prev = dfs(head->next, rehead);
          prev->next = head;
          head->next = nullptr;
          return head;
      }
  };

  //3-2:优化版本

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

2.GIF题目解析

请添加图片描述

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

在这里插入图片描述

两两交换链表中的节点

1.思路一:

在这里插入图片描述

class Solution {
   
public:
    ListNode* swapPairs(ListNode* head) {
   
        //1.没有节点和只有一个节点
        if(head==nullptr)
            return nullptr;
        if(head->next == nullptr)
            return head;
        
        //2.正常情况:
        ListNode* newnext_r = head->next->next;
        ListNode* newnext_l = head->next;
        head->next->next = head;
        head->next = swapPairs(newnext_r);
        return newnext_l;
    }
};

2.GIF题目解析

在这里插入图片描述

五.pow(X,N)-快速幂

在这里插入图片描述

pow(X,N)-快速幂

1.思路一:快速幂递归

在这里插入图片描述


 //5.pow(x,n)实现

 class Solution_5 {
   
 public:
     double myPow(double x, int n) {
   
         //1.特殊情况的处理:n值无穷小的情况!
         //2.在这个地方去处理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);
     }
 };

相关推荐

  1. 算法篇:搜索回溯算法

    2024-01-09 21:26:01       28 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-09 21:26:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-09 21:26:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-09 21:26:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-09 21:26:01       20 阅读

热门阅读

  1. 找茬小游戏开发需要清楚哪些内容呢?

    2024-01-09 21:26:01       35 阅读
  2. Python 基础(一):基本语句

    2024-01-09 21:26:01       33 阅读
  3. 贪心算法day03

    2024-01-09 21:26:01       34 阅读
  4. Redis 过期策略

    2024-01-09 21:26:01       36 阅读
  5. C++_多态(函数指针)

    2024-01-09 21:26:01       32 阅读
  6. 读史笔记(一)

    2024-01-09 21:26:01       31 阅读
  7. GPT实战系列-ChatGLM3管理外部借力工具

    2024-01-09 21:26:01       36 阅读
  8. POST请求方式解决乱码问题【Spring MVC】

    2024-01-09 21:26:01       47 阅读
  9. 【第八节】变量与运算符-整型数据类型的使用

    2024-01-09 21:26:01       33 阅读
  10. 基于长短期神经网络LSTM的测量误差预测

    2024-01-09 21:26:01       38 阅读