每天努力多一点,打开刷题,谁tm过情人节啊。
有什么比把快乐带给别人更快乐的呢hahaha
递归别扯三部曲,没有用,比如:什么条件返回应该是本能,而不是靠背过程去找。
递归
- 一、简单题的集合
-
- 1. [21. 合并两个有序链表](https://leetcode-cn.com/problems/merge-two-sorted-lists/)
- 2.[203. 移除链表元素](https://leetcode-cn.com/problems/remove-linked-list-elements/)
- 3.[206. 反转链表](https://leetcode-cn.com/problems/reverse-linked-list/)
- 4.[231. 2 的幂](https://leetcode-cn.com/problems/power-of-two/)
- 5.[234. 回文链表](https://leetcode-cn.com/problems/palindrome-linked-list/)
- 6.[509. 斐波那契数](https://leetcode-cn.com/problems/fibonacci-number/)
- 7.[剑指 Offer 06. 从尾到头打印链表](https://leetcode-cn.com/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof/)
- 8.[剑指 Offer 62. 圆圈中最后剩下的数字](https://leetcode-cn.com/problems/yuan-quan-zhong-zui-hou-sheng-xia-de-shu-zi-lcof/)
- 9.[面试题 08.06. 汉诺塔问题](https://leetcode-cn.com/problems/hanota-lcci/)
- 10.[2. 两数相加](https://leetcode-cn.com/problems/add-two-numbers/)
- 11.[24. 两两交换链表中的节点](https://leetcode-cn.com/problems/swap-nodes-in-pairs/)
- 12.[50. Pow(x, n)](https://leetcode-cn.com/problems/powx-n/)
- 13.[143. 重排链表](https://leetcode-cn.com/problems/reorder-list/)
- 14.[241. 为运算表达式设计优先级](https://leetcode-cn.com/problems/different-ways-to-add-parentheses/)
- 15.[394. 字符串解码](https://leetcode-cn.com/problems/decode-string/)
- 16.[486. 预测赢家](https://leetcode-cn.com/problems/predict-the-winner/)
- 17.[779. 第K个语法符号](https://leetcode-cn.com/problems/k-th-symbol-in-grammar/)
- 18.[894. 所有可能的满二叉树](https://leetcode-cn.com/problems/all-possible-full-binary-trees/)
- 19.[1969. 数组元素的最小非零乘积](https://leetcode-cn.com/problems/minimum-non-zero-product-of-the-array-elements/)
- 20.[面试题 08.05. 递归乘法](https://leetcode-cn.com/problems/recursive-mulitply-lcci/)
- 21.[剑指 Offer 64. 求1+2+…+n](https://leetcode-cn.com/problems/qiu-12n-lcof/)
- [22.剑指 Offer 33. 二叉搜索树的后序遍历序列](https://leetcode-cn.com/problems/er-cha-sou-suo-shu-de-hou-xu-bian-li-xu-lie-lcof/)
一、简单题的集合
1. 21. 合并两个有序链表
理解
这个题,先抽出一个最简单的动作,就是每次找一个最小的数。
给个例子:
输入:l1 = [1,2,4], l2 = [1,3,4]
输出:[1,1,2,3,4,4]
1.我们要做的是排序+存储
1.先进行从小到大排序,[1,1,2,3,4,4],排完一个位置就把这个位置放入一个类似栈的地方,先放着,等完全排完,再进行存储。
2.现在全部进栈了,先拿出4,再拿下一个4,就是这种。
代码:
class Solution {
public:
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
if(list1 == nullptr)
return list2;
else if(list2 == nullptr)
return list1;
else if(list1->val < list2->val) {
list1->next = mergeTwoLists(list1->next,list2);
return list1;
}
else {
list2->next = mergeTwoLists(list1,list2->next);
return list2;
}
}
};
2.203. 移除链表元素
理解
凡是一个while能完成的,理论上递归都可以,所以这道题,so easy。
只稍微说一下这个指针的问题,比如{1,2,3,3,4,5,6}删除的数为val = 4,到了下标4这里指针的next为{5,6},如果node->val == val那么就返回{5,6}给下标3的指针,如果node->val != val 那么就返回{4,5,6}这不就没删除嘛。(我言语不清晰haha)
代码
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
if(head == nullptr) return NULL;
head->next = removeElements(head->next,val);
return head->val == val?head->next:head;
}
};
3.206. 反转链表
理解
类似上一道题1->2->3->4->5->首先5空过,4与5对调1->2->3->4<-5,3和4对调,1->2->3<-4<-5,如此往复最后变成1<-2<-3<-4<-5。
是不是挺简单的,反正递归进行的时候每一个栈已经找到了每一个结点(我不会说哈,描述有大问题,我去评论区找了给个大佬的描述😊作者:NoColor96)
代码
class Solution {
/*
第一轮出栈,head为5,head.next为空,返回5
第二轮出栈,head为4,head.next为5,执行head.next.next=head也就是5.next=4,
把当前节点的子节点的子节点指向当前节点
此时链表为1->2->3->4<->5,由于4与5互相指向,所以此处要断开4.next=null
此时链表为1->2->3->4<-5
返回节点5
第三轮出栈,head为3,head.next为4,执行head.next.next=head也就是4.next=3,
此时链表为1->2->3<->4<-5,由于3与4互相指向,所以此处要断开3.next=null
此时链表为1->2->3<-4<-5
返回节点5
第四轮出栈,head为2,head.next为3,执行head.next.next=head也就是3.next=2,
此时链表为1->2<->3<-4<-5,由于2与3互相指向,所以此处要断开2.next=null
此时链表为1->2<-3<-4<-5
返回节点5
第五轮出栈,head为1,head.next为2,执行head.next.next=head也就是2.next=1,
此时链表为1<->2<-3<-4<-5,由于1与2互相指向,所以此处要断开1.next=null
此时链表为1<-2<-3<-4<-5
返回节点5
出栈完成,最终头节点5->4->3-2->1
*/
public:
ListNode* reverseList(ListNode* head) {
if(head == nullptr || head->next == nullptr) return head;
ListNode* dududu = reverseList(head->next);
head->next->next = head;
head->next = nullptr;
return dududu;
}
};
4.231. 2 的幂
理解
🐱🐉🐱🐉🐱🐉有手就行吧。
代码
class Solution {
public:
bool isPowerOfTwo(int n) {
if(n == 0) return false;
else if(n == 1) return true;
else if(n%2 == 1) return false;
return isPowerOfTwo(n/2);
}
};
一模一样的题型:
class Solution {
public:
bool isPowerOfThree(int n) {
if(n==0) return false;
else if(n == 1) return true;
else if(n%3 == 1||n%3==2) return false;
return isPowerOfThree(n/3);
}
};
2.342. 4的幂
class Solution {
public:
bool isPowerOfFour(int n) {
if(n==0) return false;
else if(n==1) return true;
else if(n%4 == 1 || n%4 == 2||n%4==3) return false;
return isPowerOfFour(n/4);
}
};
5.234. 回文链表
理解
递归可以让链表倒着输出,所以也可以让链表倒着比较。😁😁这答案这不手到擒来。
原理就是先用递归把跑到尾,然后从尾一个个退出来,同时头一个个向后走。
代码
class Solution {
public:
bool isPalindrome(ListNode* head) {
ListNode* end = head;
return equle_head_end(end,head);
}
bool equle_head_end(ListNode* end,ListNode* &head){
if(end == nullptr) return true; //这个条件是出递归的条件,我们放入递归使用的是end,所以也是end当作出递归的条件。
bool answer = equle_head_end(end->next,head);
answer = answer && end->val == head->val;
head = head->next;
return answer;
}
};
6.509. 斐波那契数
理解
很经典。
代码
class Solution {
public:
int fib(int n) {
if(n < 2) return n;
return fib(n-1) + fib(n-2);
}
};
7.剑指 Offer 06. 从尾到头打印链表
理解
是倒着输出的题哦。