当前位置: 首页 > news >正文

刷爆leetcode Day11 DFS

DFS

  • 1. 汉诺塔(easy)
  • 2. 合并两个有序链表(easy)
  • 3. 反转链表(easy)
  • 4. 两两交换链表中的节点(medium)
  • 5. Pow(x,n)-快速幂(medium)

1. 汉诺塔(easy)

https://leetcode.cn/problems/hanota-lcci/description/

class Solution {
public:void hanota(vector<int>& A, vector<int>& B, vector<int>& C) {//A柱借助B柱移动到C柱//用递归解决问题,不要过多关注具体细节//首先拆分子问题//其次解决子问题//返回(回溯条件)_hanota(A,B,C,A.size());}void _hanota(vector<int>& A, vector<int>& B, vector<int>& C,int n){if(n==1)//只剩一个盒子这个盒子是最大的{C.push_back(A.back());A.pop_back();return;}//子问题,将n-1个盒子借助C挪到B_hanota(A,C,B,n-1);C.push_back(A.back());A.pop_back();//将n-1个盒子借助借助A挪到C_hanota(B,A,C,n-1);}
};

2. 合并两个有序链表(easy)

https://leetcode.cn/problems/merge-two-sorted-lists/submissions/576440296/

/*** 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(list2->next,list1);return list2;}}
};

3. 反转链表(easy)

https://leetcode.cn/problems/merge-two-sorted-lists/submissions/576419884/

/*** 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)return nullptr;//链表可能为空if(head->next==nullptr)return head;ListNode*NewNode=reverseList(head->next);head->next->next=head;head->next=nullptr; return NewNode;}
};

4. 两两交换链表中的节点(medium)

https://leetcode.cn/problems/swap-nodes-in-pairs/submissions/576438012/

/*** 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)return nullptr;if(head->next==nullptr) return head;ListNode*tmp=swapPairs(head->next->next);ListNode*ret=head->next;head->next->next=head;head->next=tmp;return ret;}
};

5. Pow(x,n)-快速幂(medium)

https://leetcode.cn/problems/powx-n/submissions/576439853/

class Solution {
public:double _myPow(double x, int n){if(n==0)return 1.0;double tmp=_myPow(x,n/2);return n%2==0?tmp*tmp:x*tmp*tmp;}double myPow(double x, int n) {return n>=0?_myPow(x,n):1.0/_myPow(x,n);}
};
http://www.lryc.cn/news/471123.html

相关文章:

  • 虚拟机不同网络模式的区别
  • 嵌入式软件 Bug 排查与调试技巧
  • 阿里云环境下用docker搭建redis主从复制
  • STM32 从0开始系统学习 1
  • python-numpy-笔记1
  • 云+AI 时代的 OceanBase
  • 【C++】vector使用详解
  • .NET Core WebApi第5讲:接口传参实现、数据获取流程、204状态码问题
  • 运维面试汇总
  • 学习封装Flutter组件,看这篇就够了
  • 无线麦克风方案芯片DSH32F3024
  • 谷粒商城の秒杀服务
  • 庆祝程序员节:聊一聊编程语言的演变
  • 大模型技术在网络安全领域的应用与发展
  • 基于vite和vue3、 eslint、prettier、stylelint、husky规范
  • git push到远程怎么回退
  • Web保存状态的手段(Application的使用)
  • 高翔【自动驾驶与机器人中的SLAM技术】学习笔记(十二)拓展图优化库g2o(一)框架
  • Flutter Row组件实战案例
  • 【ubuntu20.04】【ROS Noetic】【ROS安装】【Website may be down.】【gpg: 找不到有效的 OpenPGP 数据。】
  • Python开发必备,这些黑科技库你get到了吗
  • sublime text 常用快捷键
  • Kubernetes(K8S) + Harbor + Ingress 部署 SpringBoot + Vue 前后端分离项目
  • 【iOS】知乎日报第一周总结
  • Springboot整合spring-boot-starter-data-elasticsearch
  • 【大模型系列】mPLUG-Owl3(2024.08)
  • 从0到1学习node.js(express模块)
  • MambaVision
  • MySQLDBA修炼之道-开发篇(二)
  • 前端必备的环境搭建