![](https://img-blog.csdnimg.cn/20190927151117521.png?x-oss-process=image/resize,m_fixed,h_224,w_224)
递归/递推
文章平均质量分 70
递归和递推算法
memcpy0
希望探索文理结合的自由之路。
展开
-
LeetCode 1080. Insufficient Nodes in Root to Leaf Paths【递归,二叉树】中等
本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。。原创 2023-05-22 19:44:56 · 445 阅读 · 0 评论 -
洛谷 P5743 【深基7.习8】猴子吃桃【递推/递归/数学】
https://memcpy0.blog.csdn.net/article/details/121326963原创 2021-11-15 14:27:50 · 3595 阅读 · 0 评论 -
LeetCode 38. Count and Say【字符串/迭代/递归/模拟/打表】简单
The count-and-say sequence is the sequence of integers with the first five terms as following:1. 12. 113. 214. 12115. 1112211 is read off as “one 1” or 11.11 is read off as “two 1s” or 21.21 is read off as "one 2", then "one 1"原创 2020-06-13 01:30:16 · 291 阅读 · 0 评论 -
LeetCode 37. Sudoku Solver【回溯/递归/数组】困难
本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。为了方便在PC上运行调试、分享代码文件,我还建立了相关的仓库:https://github.com/memcpy0/LeetCode-Conqu.原创 2021-10-02 00:35:03 · 203 阅读 · 0 评论 -
LeetCode 430. Flatten a Multilevel Doubly Linked List【链表/DFS/递归/迭代】中等
You are given a doubly linked list which in addition to the next and previous pointers, it could have a child pointer, which may or may not point to a separate doubly linked list. These child lists may have one or more children of their own, and so on, to原创 2021-09-28 10:16:19 · 306 阅读 · 0 评论 -
PAT 团体程序设计天梯赛-练习集 L2-011 玩转二叉树 (25 分)
给定一棵二叉树的中序遍历和前序遍历,请你先将树做个镜面反转,再输出反转后的层序遍历的序列。所谓镜面反转,是指将所有非叶结点的左右孩子对换。这里假设键值都是互不相等的正整数。输入格式:输入第一行给出一个正整数 NNN(≤30≤30≤30),是二叉树中结点的个数。第二行给出其中序遍历序列。第三行给出其前序遍历序列。数字间以空格分隔。输出格式:在一行中输出该树反转后的层序遍历的序列。数字间以1个空格分隔,行首尾不得有多余空格。输入样例:71 2 3 4 5 6 74 1 3 2 6 5 7输出原创 2021-05-28 06:49:09 · 145 阅读 · 0 评论 -
LeetCode C++ 1545. Find Kth Bit in Nth Binary String【String/Recursion】中等
Given two positive integers n and k, the binary string Sn is formed as follows: S1 = "0" Si = Si-1 + "1" + reverse(invert(Si-1)) for i > 1Where + denotes the concatenation opera原创 2021-05-23 03:34:40 · 175 阅读 · 1 评论 -
LeetCode C++ 1863. Sum of All Subset XOR Totals【BIt Manipulation/Recursion】简单
The XOR total of an array is defined as the bitwise XOR of all its elements, or 0 if the array is empty.For example, the XOR total of the array [2,5,6] is 2 XOR 5 XOR 6 = 1.Given an array nums, return the sum of all XOR totals for every subset of nums.&n原创 2021-05-18 01:21:59 · 483 阅读 · 1 评论 -
LeetCode C++ 889. Construct Binary Tree from Preorder and Postorder Traversal【Tree/Recursion】中等
Return any binary tree that matches the given preorder and postorder traversals.Values in the traversals pre and post are distinct positive integers.Example 1:Input: pre = [1,2,4,5,3,6,7], post = [4,5,2,6,7,3,1]Output: [1,2,3,4,5,6,7]原创 2021-05-07 03:31:31 · 150 阅读 · 1 评论 -
LeetCode C++ 784. Letter Case Permutation【String/Bit Manipulation/Backtracking】中等
Given a string S, we can transform every letter individually to be lowercase or uppercase to create another string.Return a list of all possible strings we could create. You can return the output in any order.Example 1:Input: S = "a1b2"Output原创 2021-05-05 03:24:17 · 189 阅读 · 0 评论 -
LeetCode C++ 剑指 Offer 19. 正则表达式匹配【动态规划/递归】困难
请实现一个函数用来匹配包含'. '和'*'的正则表达式。模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(含0次)。在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串"aaa"与模式"a.a"和"ab*ac*a"匹配,但与"aa.a"和"ab*a"均不匹配。示例 1:输入:s = "aa"p = "a"输出: false解释: "a" 无法匹配 "aa" 整个字符串。示例 2:输入:s = "aa"p = "a*"输出: true解释: 因为 '原创 2021-03-19 23:45:33 · 337 阅读 · 0 评论 -
LeetCode C++ 814. Binary Tree Pruning【Tree/Recursion】中等
We are given the head node root of a binary tree, where additionally every node's value is either a 0 or a 1.Return the same tree where every subtree (of the given tree) not containing a 1 has been removed.(Recall that the subtree of a node X is X,原创 2021-01-18 18:34:46 · 163 阅读 · 0 评论 -
LeetCode C++ 面试题 08.05. Recursive Mulitply LCCI【Recursion/Math】中等
Write a recursive function to multiply two positive integers without using the * operator. You can use addition, subtraction, and bit shifting, but you should minimize the number of those operations.Example 1:Input: A = 1, B = 10Output: 10Example 2:I原创 2020-11-27 00:37:24 · 291 阅读 · 0 评论 -
LeetCode C++ 50. Pow(x, n)【Recursion】中等
Implement pow(x, n), which calculates x raised to the power n (i.e. xn).Example 1:Input: x = 2.00000, n = 10Output: 1024.00000Example 2:Input: x = 2.10000, n = 3Output: 9.26100Example 3:Input: x = 2.00000, n = -2Output: 0.25000Explanation:原创 2020-11-26 02:11:58 · 286 阅读 · 0 评论 -
LeetCode C++ 剑指 Offer 16. 数值的整数次方【Math/Recursion】中等
实现函数double Power(double base, int exponent),求base的exponent次方。不得使用库函数,同时不需要考虑大数问题。示例 1:输入: 2.00000, 10输出: 1024.00000示例 2:输入: 2.10000, 3输出: 9.26100示例 3:输入: 2.00000, -2输出: 0.25000解释: 2-2 = 1/22 = 1/4 = 0.25说明: -100.0 < x原创 2020-11-26 01:59:54 · 248 阅读 · 0 评论 -
LeetCode C++ 面试题 04.02. Minimum Height Tree LCCI【Tree/Recursion】简单
Given a sorted (increasing order) array with unique integer elements, write an algorithm to create a binary search tree with minimal height.Example:Given sorted array: [-10,-3,0,5,9],One possible answer is: [0,-3,9,-10,null,5],which represents the fol原创 2020-11-24 01:22:01 · 171 阅读 · 0 评论 -
LeetCode C++ 面试题 02.06. Palindrome Linked List LCCI【Linked List/Recursion】简单
Implement a function to check if a linked list is a palindrome.Example 1:Input: 1->2Output: false Example 2:Input: 1->2->2->1Output: true Follow up: Could you do it in O(n) time and O(1) space?题意:编写一个函数,检查输入的链表是否是回文的。解法1 辅助空间使用原创 2020-11-23 23:06:37 · 154 阅读 · 0 评论 -
LeetCode C++ 面试题 08.06. Hanota LCCI【Stack/Recursion】简单
In the classic problem of the Towers of Hanoi, you have 3 towers and N disks of different sizes which can slide onto any tower. The puzzle starts with disks sorted in ascending order of size from top to bottom (i.e., each disk sits on top of an even larger原创 2020-11-17 15:18:39 · 247 阅读 · 0 评论 -
LeetCode C++ 面试题 17.12. BiNode LCCI【Tree/Recursion】简单
The data structure TreeNode is used for binary tree, but it can also used to represent a single linked list (where left is null, and right is the next node in the list). Implement a method to convert a binary search tree (implemented with TreeNode) into a原创 2020-11-15 15:12:16 · 187 阅读 · 0 评论 -
LeetCode C++ 剑指 Offer 25. 合并两个排序的链表【Linked List/Recursion】简单
输入两个递增排序的链表,合并这两个链表并使新链表中的节点仍然是递增排序的。示例1:输入:1->2->4, 1->3->4输出:1->1->2->3->4->4限制: 0 <= 链表长度 <= 1000解法1 迭代归并class Solution {public: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { if (!l1 || !l2)原创 2020-11-13 19:50:07 · 162 阅读 · 0 评论 -
LeetCode C++ 剑指 Offer 27. 二叉树的镜像【Tree】简单
请完成一个函数,输入一个二叉树,该函数输出它的镜像。例如输入: 4 / \ 2 7 / \ / \1 3 6 9镜像输出: 4 / \ 7 2 / \ / \9 6 3 1示例 1:输入:root = [4,2,7,1,3,6,9]输出:[4,7,2,9,6,3,1]限制: 0 <= 节点个数 <= 1000解法1 递归遍历class Solution {public:原创 2020-11-12 11:30:47 · 713 阅读 · 1 评论 -
LeetCode C++ 938. Range Sum of BST【Tree/Recursion】简单
Given the root node of a binary search tree, return the sum of values of all nodes with value between L and R (inclusive).The binary search tree is guaranteed to have unique values.Example 1:Input: root = [10,5,15,3,7,null,18], L = 7, R = 15Output: 32原创 2020-10-23 21:41:18 · 150 阅读 · 0 评论 -
LeetCode C++ 530. Minimum Absolute Difference in BST【Binary Search Tree/Recursion】简单
Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes.Example:Input: 1 \ 3 / 2Output:1Explanation:The minimum absolute difference is 1, which is the difference原创 2020-10-18 12:49:04 · 148 阅读 · 0 评论 -
LeetCode C++ 783. Minimum Distance Between BST Nodes【Binary Search Tree/Recursion】简单
Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the values of any two different nodes in the tree.Example :Input: root = [4,2,6,1,3,null,null]Output: 1Explanation:Note that root is a TreeNode object, not原创 2020-10-18 12:42:59 · 177 阅读 · 0 评论 -
LeetCode C++ 147. Insertion Sort List【排序/链表/递归】中等
Sort a linked list using insertion sort.A graphical example of insertion sort. The partial sorted list (black) initially contains only the first element in the list.With each iteration one element (red) is removed from the input data and inserted in-pl原创 2020-09-17 19:15:28 · 154 阅读 · 0 评论 -
LeetCode C++ 21. Merge Two Sorted Lists【链表】简单
Merge two sorted linked lists and return it as a new sorted list. The new list should be made by splicing together the nodes of the first two lists.Example:Input: 1->2->4, 1->3->4Output: 1->1->2->3->4->4题意:将两个升序链表合并为一个新的升序链表原创 2020-09-17 15:34:15 · 150 阅读 · 0 评论 -
LeetCode 437. Path Sum III【递归/前缀和/哈希表/回溯】中等
You are given a binary tree in which each node contains an integer value.Find the number of paths that sum to a given value.The path does not need to start or end at the root or a leaf, but it must go downwards (traveling only from parent nodes to child原创 2020-07-08 16:01:18 · 356 阅读 · 0 评论 -
LeetCode C++ 491. Increasing Subsequences【DFS】中等
Given an integer array, your task is to find all the different possible increasing subsequences of the given array, and the length of an increasing subsequence should be at least 2 .Example:Input: [4, 6, 7, 7]Output: [[4, 6], [4, 7], [4, 6, 7], [4, 6, 7原创 2020-08-27 01:39:04 · 199 阅读 · 0 评论 -
LeetCode C++ 17. Letter Combinations of a Phone Number【DFS/Backtracking】中等
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.Examp原创 2020-08-27 01:24:10 · 302 阅读 · 0 评论 -
LeetCode C++ 257. Binary Tree Paths【Tree】简单
Given a binary tree, return all root-to-leaf paths.Note: A leaf is a node with no children.Example:Input: 1 / \2 3 \ 5Output: ["1->2->5", "1->3"]Explanation: All root-to-leaf paths are: 1->2->5, 1->3题意:给定一个二叉树,返回所有从原创 2020-08-26 23:27:55 · 232 阅读 · 0 评论 -
LeetCode C++ 226. Invert Binary Tree【Tree】简单
Invert a binary tree.Example:Input: 4 / \ 2 7 / \ / \1 3 6 9Output: 4 / \ 7 2 / \ / \9 6 3 1Trivia:This problem was inspired by this original tweet by Max Howell:Google: 90% of our engineers use the原创 2020-08-26 19:19:42 · 237 阅读 · 0 评论 -
LeetCode C++ 24. Swap Nodes in Pairs【链表】中等
Given a linked list, swap every two adjacent nodes and return its head.You may not modify the values in the list’s nodes, only nodes itself may be changed.Example:Given 1->2->3->4, you should return the list as 2->1->4->3.题意:给定一个链表,两原创 2020-08-23 14:50:13 · 223 阅读 · 0 评论 -
LeetCode C++ 203. Remove Linked List Elements【链表】简单
Remove all elements from a linked list of integers that have value val.Example:Input: 1->2->6->3->4->5->6, val = 6Output: 1->2->3->4->5题意:从一个单链表中删除所有等于给定值的结点。思路1:迭代删除+无虚拟头结点。代码:class Solution {public: ListNode*原创 2020-08-23 13:15:07 · 187 阅读 · 0 评论 -
LeetCode 206. 反转链表, LCR 024. 反转链表【链表,递归】简单
Reverse a singly linked list.Example:Input: 1->2->3->4->5->NULLOutput: 5->4->3->2->1->NULLFollow up:A linked list can be reversed either iteratively or recursively. Could you implement both?题意:递归或者迭代地翻转一个单链表。思路1:迭代翻转单原创 2020-08-23 00:23:49 · 301 阅读 · 1 评论