解题思路
递归是我一直以来的坎, 一看就会, 一写就废.
今天总结一下, 主要分四步:
设置停止条件, 防止死归
递之前的操作
传递的操作
归回来的操作
递归的微妙之处:
代码简短到像在和计算机交流
只在递之前操作, 就像时光流逝, 从起点到终点顺向执行
只在归回来操作, 就像时光倒流, 从终点回起点逆向执行
即递下去, 也归回来. 我现在还说不出来精髓, 但是有一段代码写的很好, 在这里分享给大家.
作者:charlie-o4
链接:https://leetcode-cn.com/problems/reverse-string/solution/di-gui-jie-fa-javaban-by-charlie-o4-5xto/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
class Solution {
public:
int fib(int n) {
if (n == 0) return 0;
if (n == 1) return 1;
return fib(n - 1) + fib(n - 2);
}
};
func fib(n int) int {
if n < 2 {
return n
}
p, q, r := 0, 0, 1
for i := 2; i <= n; i++ {
p = q
q = r
r = p + q
}
return r
}
作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/fibonacci-number/solution/fei-bo-na-qi-shu-by-leetcode-solution-o4ze/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
//动态规划:滚动思想
class Solution {
public:
int fib(int n) {
if (n < 2) {
return n;
}
int p = 0, q = 0, r = 1;
for (int i = 2; i <= n; ++i) {
p = q;
q = r;
r = p + q;
}
return r;
}
};
反转链表的递归
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if (!head || !head->next) {
return head;
}
ListNode* newHead = reverseList(head->next);
head->next->next = head;
head->next = nullptr;
return newHead;
}
};
作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/reverse-linked-list/solution/fan-zhuan-lian-biao-by-leetcode-solution-d1k2/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
func reverseList(head *ListNode) *ListNode {
if head == nil || head.Next == nil {
return head
}
newHead := reverseList(head.Next)
head.Next.Next = head
head.Next = nil
return newHead
}
作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/reverse-linked-list/solution/fan-zhuan-lian-biao-by-leetcode-solution-d1k2/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
//迭代的滚动
class Solution {
public ListNode reverseList(ListNode head) {
ListNode prev = null;
ListNode curr = head;
while (curr != null) {
ListNode next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
}
}
344:反转字符串
class Solution {
char temp = 0;
public void reverseString(char[] s) {
reverse(s, 0, s.length-1);
}
public void reverse(char[]s, int left, int right) {
// 返回条件
if (left>=right) {
return;
}
// 递之前
temp = s[left];
s[left] = s[right];
s[right] = temp;
// 递下去
reverse(s, left+1, right-1);
// 归回来
// Do nothing.
}
}
作者:charlie-o4
链接:https://leetcode-cn.com/problems/reverse-string/solution/di-gui-jie-fa-javaban-by-charlie-o4-5xto/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
687:最长同值路径
纯面向对象的语言(c#),只要无脑定义val,left和right就好了,只需要注意其类型,val是数值类型;left和right是类类型。
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int val=0, ListNode next=null) {
* this.val = val;
* this.next = next;
* }
* }
*/
go:
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
????