当前位置:网站首页>Day10/11 递归 / 回溯
Day10/11 递归 / 回溯
2022-06-10 14:51:00 【寄生于黑暗中的光】
1 21. 合并两个有序链表
1.1 题目

1.2 题解
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */
class Solution {
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
if(list1 == null)
return list2;
if(list2 == null)
return list1;
ListNode mergeList = new ListNode();
ListNode res = mergeList;
ListNode p = list1;
ListNode q = list2;
while(p != null && q != null){
if(p.val < q.val){
mergeList.next = p;
mergeList = mergeList.next;
p = p.next;
}else{
mergeList.next = q;
mergeList = mergeList.next;
q = q.next;
}
}
if(p == null && q == null)
return res;
while(p != null){
mergeList.next = p;
mergeList = mergeList.next;
p = p.next;
}
while(q != null){
mergeList.next = q;
mergeList = mergeList.next;
q = q.next;
}
return res.next;
}
}
注:自己采用的归并的思想,但是这道题本身还是以递归的思路去做较为合适,这篇文章的题解很好。
2 206. 反转链表
2.1 题目

2.2 题解
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */
class Solution {
public ListNode reverseList(ListNode head) {
if(head == null)
return head;
ListNode p = new ListNode(head.val, null);
while(head.next != null){
ListNode q = new ListNode(head.next.val, null);
q.next = p;
p = q;
head = head.next;
}
return p;
}
}
注:第一想法就是头插法,也实现了;看了下题解,递归好重要。
3 77. 组合
3.1 题目

3.2 题解
class Solution {
public List<List<Integer>> combine(int n, int k) {
List<List<Integer>> res = new ArrayList<>();
if(k <= 0 || k > n)
return res;
// 从1开始搜索
Deque<Integer> path = new ArrayDeque<>();
dfs(n, k, 1, path, res);
return res;
}
private void dfs(int n, int k, int begin, Deque<Integer> path, List<List<Integer>> res){
// 确定递归的终止条件:栈内长度达到组合长度时
if(path.size() == k){
res.add(new ArrayList<>(path));
return;
}
// 遍历所有的搜索起点
for(int i = begin; i <= n - (k - path.size()) + 1; i++){
// 向路径变量中添加一个数
path.addLast(i);
// 下一轮搜索,设置的搜索起点要加 1,因为组合数理不允许出现重复的元素
dfs(n, k, i + 1, path, res);
// 重点理解这里:深度优先遍历有回头的过程,因此递归之前做了什么,递归之后需要做相同操作的逆向操作
path.removeLast();
}
}
}
注:这个大佬的回溯题解好好学。
4 46. 全排列
4.1 题目

4.2 题解
class Solution {
public List<List<Integer>> permute(int[] nums) {
int len = nums.length;
List<List<Integer>> res = new ArrayList<>();
if(len == 0)
return res;
// 记录每一次排列中元素进path的信息
boolean[] used = new boolean[len];
List<Integer> path = new ArrayList<>();
dfs(nums, len, 0, path, used, res);
return res;
}
private void dfs(int[] nums, int len, int depth, List<Integer> path, boolean[] used, List<List<Integer>> res){
// 递归终止条件:depth==len,也就是说所有的数已经取完
if(depth == len){
res.add(new ArrayList<>(path));
return;
}
// 在还未选择的数中依次选择一个元素作为下一个位置的元素
for(int i = 0; i < len; i++){
// 如果该元素并未进path
if(!used[i]){
path.add(nums[i]);
used[i] = true;
// 继续向下搜索直至path满为止
dfs(nums, len, depth + 1, path, used, res);
used[i] = false;
path.remove(path.size() - 1);
}
}
}
}
注:题解好好看。
5 784. 字母大小写全排列
5.1 题目

5.2 题解
class Solution {
public List<String> letterCasePermutation(String s) {
List<String> res = new ArrayList<>();
char[] path = new char[s.length()];
dfs(0, s.toCharArray(), res);
return res;
}
private void dfs(int index, char[] s, List<String> res){
if(index == s.length){
res.add(new String(s));
return;
}
if(Character.isLetter(s[index])) {
//用Character.isLetter()判断是否为字母,很方便
s[index] = Character.toLowerCase(s[index]);
dfs(index + 1, s, res); //对于字母保证生成小写的和大写的两种情况
s[index] = Character.toUpperCase(s[index]);
} //分别用Character.toLowerCase()和Character.toUpperCase()转化大小写字母,再也不用写判断手动转化啦
dfs(index + 1, s, res); //不管是数字还是字母都要进入下一个字符
}
}
注:这道题的细节没搞清楚,题解在此。
6 总结
感觉不管是递归还是回溯问题,都有一定的模板,等刷完所有的题型回来复习这些题,猛刷同类型。
边栏推荐
- 洞見科技入選「愛分析· 隱私計算廠商全景報告」,獲評金融解决方案代錶廠商
- 2022南京国际智慧工地装备展览会
- AutoRunner自动化测试工具如何创建项目-Alltesting|泽众云测试
- 一文带你了解J.U.C的FutureTask、Fork/Join框架和BlockingQueue
- LeetCode_21(合并两个有序链表)
- ScrollView 初始化的时候不在最顶部?
- Golang []byte 转 File
- CG collision testing
- [registration] to solve the core concerns of technology entrepreneurs, the online enrollment of "nebula plan open class" was opened
- [discrete mathematics review series] VI. tree
猜你喜欢

Shutter wrap button bottomnavigationbar learning summary 4
![[logodetection data set processing] (1) divide the data set into training set and verification set](/img/f9/422cd4b710cd57f0ebb3abd7e01c29.png)
[logodetection data set processing] (1) divide the data set into training set and verification set
![[registration] to solve the core concerns of technology entrepreneurs, the online enrollment of](/img/d7/4671b5a74317a8f87ffd36be2b34e1.jpg)
[registration] to solve the core concerns of technology entrepreneurs, the online enrollment of "nebula plan open class" was opened

【LogoDetection 数据集处理】(4)提取每张图片的logo区域

2022 the 15th Nanjing International Digital Industry Expo

这个牛逼的低代码生成器,现在开源了!
![[discrete mathematics review series] i. propositional logic](/img/ae/7f062cfa416a26be3d32dfb1353c80.png)
[discrete mathematics review series] i. propositional logic

KaTeX问题 —— csdn编辑时中打出等号对齐的样式

Mutual transformation among lists, arrays and tensors

Create a space of local value together. In 2022, China successfully held the "one hundred cities tour · Ningbo Station" for commercial distribution
随机推荐
Mutual transformation among lists, arrays and tensors
共创地市价值空间,2022年华为商业分销地市百城行·宁波站成功举办
100003 words, take you to decrypt the system architecture under the double 11 and 618 e-commerce promotion scenarios
[discrete mathematics review series] i. propositional logic
【LogoDetection 数据集处理】(3)将训练集按照类别划分为多个文件夹
初识RPC
详解OpenCV的函数filter2D(),并提醒大家它做的运算并不是卷积运算而是相关运算
小程序警告:Now you can provide attr `wx:key` for a `wx:for` to improve performance.
.NET C#基础(7):接口 - 人如何和猫互动
微信小程序 关闭当前页面
js获取数组中最大值
2022第十五届南京国际工业自动化展览会
利用 GDB 快速阅读 postgresql 的内核代码
[logodetection data set processing] (3) divide the training set into multiple folders by category
CG碰撞检测 Collision Testing
[cloud native | kubernetes] in depth RC, RS, daemonset, statefulset (VII)
Scrollview is not at the top during initialization?
消息中间件的消费模式
Golang []byte 转 File
CVPR 2022 | frame by frame motion representation of long video based on sequence contrast learning