递归

递归往往面对一类问题时,如果它的规模足够小或者说达到既定的边界条件时,我们可以直接获取答案。但是,当这类问题的规模比较大时,却往往无法直接获取答案。那么,这个时候就可以通过“自身调用自身”的方式,来不断地减小问题的规模,直到问题的规模被缩减到足够小时,直接将答案返回上层的调用者,最终获取到原问题的解。如果将求解的过程逆过来,那么就是所谓的递推。点击 阅读全文 > Read More

《DATA STRUCTRUES A Psuedocode Approach with C++》Chaper 3. Linked List Learn Note

3-1 LINEAR LIST CONCEPTLinear lists can be divided into two categories: general and restricted.In a general list, data can be inserted and deleted anywhere and there are no restrictions on the operations that can be used to process the list. Such as the random list, ordered list.In a restricted list, data can only be added or deleted at the ends of the structure and processing is restricted to op... Read More

Protected: 2019算法与数据结构学习清单

字符串最长公共子串(Longest Common Substring)最长公共子序列(Longest Common Sequence)字符串 & 文件内容Diff滚动哈希(Rolling Hash)- Rabin-Karp string-searchingrsyncSuffix arrayMinimal string(最小表示法)数论负进制(如:-2进制)的转换与加法运算图论Prim's Minimum Spanning Tree(MST Read More

小旭讲解 LeetCode 399. Evaluate Division 并查集

原题英文 —— Evaluate DivisioEquations are given in the formatA / B = k, whereAandBare variables represented as strings, andkis a real number (floating point number). Given some queries, return the answers. If the answer does not exist, return-1.0.Example:Givena / b = 2.0, b / c = 3.0.queries are:a / c = ?, b / a = ?, a / e = ?, a / a = ?, x / x = ? .return[6.0, 0.5, -1.0, 1.0, -1.0 ].The input is:vec... Read More

小旭讲解 LeetCode 39. Combination Sum 回溯法

原题Given asetof candidate numbers (candidates)(without duplicates)and a target number (target), find all unique combinations incandidateswhere the candidate numbers sums totarget.Thesamerepeated number may be chosen fromcandidatesunlimited number of times.NoteAll numbers (includingtarget) will be positive integers.The solution set must not contain duplicate combinations.Example 1Input: candidates... Read More

动态规划与分治法的思考

如果一个问题具有最优子结构的性质,此外子问题具有重叠性质,那么可以采用自底向上的动态规划的思路进行求解。同时,往往可以用递归的方式自顶向地进行求解,即分治法。如果用分治法去求解这个问题时,能够利用备忘录法进行避免对于子问题的重复计算,那么其计算的效率可以和动态规划的计算效率相比。文章来源:胡小旭 =>动态规划与分治法的思考 Read More

分治法与回溯法的思考

共同的递归性质在广义上来说,所有递归的算法都属于分治法。无非是将问题分解成一个规模更小的问题,还是将问题分解成若干个,甚至和输入规模多项式级别的子问题。那么对于前者,有些作者称作是减治法,后者称作分治法。那么对于回溯法(以深度优先搜搜方式进行)来说,目前为止我见过的都是通过递归的形式来实现的,那么从这个意义上来讲,回溯算法就是分治法的一种。回溯状态的有无再说,之所以称作是回溯法,是因为在搜索的过程中需要回溯到问题的某个状态,所以这往往需要保存回溯时的一些状态属性。然而,分治法通常并不需要考虑回溯状态的保存。分解问题的规模分治法往往是将问题分解成若干个子问题的形式,然而回溯法往往是将问题分解成规模更小的一个子问题。由于分治法将问题分解成若干个子问题,故当前问题的解需要依赖于若干个子问题,也就是若干个搜索路径的解,所以重点在于如何合并子问题的解;而然回溯法问题规模就为1个,当前的搜索路径的问题... Read More

一只青蛙跳出来的分治法、回溯法与动态规划

从2018年7月份开始,基础薄弱的我从0开始刷LeetCode题目。目的性很明确,也很简单——就是为了提高解决问题的思考实践能力,也为了提升自己的核心竞争力。也许,牛人会觉得这并不算什么竞争力。是的,我同意的。但,这是我目前能做的比较基础的事情罢了。迄今(2018年12月28日)为止,已经刷了108道题目。顺序基本上是按照出现的频率(Frequency)来刷的,这个频率在LeetCode上需要订阅后才可以看得到。那么在刷了108道题目后,有那么一些题目会觉得“似曾相识”了,也会有一种触类旁通的感觉了。所以,我觉得应该适当放慢刷题的速度,同时做做总结了。所以,计划了一项视频解说计划,在YouTubeh和B站都建立了《小旭解说算法之路》的频道,欢迎订阅,多多提建议。那么,进入正题。经过了108道题的历练之后,我来说说对于分治法、回溯法和动态规划的理解。我觉得他们三者是一个相互有交集的概念,并不... Read More

小旭讲解 LeetCode 53. 最大子序和 动态规划 分治策略

原题Given an integer arraynums, find the contiguous subarray(containing at least one number) which has the largest sum and return its sum.ExampleInput: [-2,1,-3,4,-1,2,1,-5,4]Output: Explanation:[4,-1,2,1] has the largest sum = 6.Follow upIf you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.题解动态规划哔哩哔哩 动态规划YouTube 动态规划通过把原问题分解为相对简单的子问题的方式求解复杂问题的方法。... Read More

[LeetCode]743. Network Delay Time

原题There are N network nodes, labelled 1 to N.Given times, a list of travel times as directed edges times[i] = (u, v, w), where u is the source node, v is the target node, and w is the time it takes for a signal to travel from source to target.Now, we send a signal from a certain node K. How long will it take for all nodes to receive the signal? If it is impossible, return -1.NoteN will be in the ... Read More

[LeetCode]893. Groups of Special-Equivalent Strings

原题You are given an array A of strings.Two strings S and T are special-equivalent if after any number of moves, S == T.A move consists of choosing two indices i and j with i % 2 == j % 2, and swapping S[i] with S[j].Now, a group of special-equivalent strings from A is a non-empty subset S of A such that any string not in S is not special-equivalent with any string in S.Return the number of groups o... Read More

[LeetCode]Valid Palindrome II

原题Given a non-empty string s, you may delete at most one character. Judge whether you can make it a palindrome.Example 1Input: "aba"Output: TruExample 2Input: "abca"Output: TruExplanation: You could delete the character 'c'.NoteThe string will only contain lowercase characters a-z. The maximum length of the string is 50000.思路水题。判断是否是回文字符串最简单的方式就是从左右两端逐步向中心逼近(双指针),如下代码[code lang="cpp"for (in... Read More

最大流问题之Ford-Fulkerson算法

Ford-Fulkerson算法(亦即标号法?)的输入与步骤如下输入给定一个容量为c的图G=(V, E),源点s与汇点(终点)步骤对图G中每一个边(u, v)的流量f(u, v)进行初始化为查询过程:寻找(DFS、深度优先搜索方式)图G中的一条路径p,其中每一条边(u, v) ∈p,都有fc(u, v) = c(u, v) - f(u, v) > 0(c(u, v) 代表当前边的容量,f(u, v) 代表当前边已有的流量,即c(u, v) - f(u, v)代表当前边可用的最大流量,即剩余流量调整过程:计算当前路径下每条边的最小剩余容量,cf(p) = min{fc(u, v) : (u, v) ∈p},然后对于每条边进行如下操作f(u, v) = f(u, v) + cf(p) (前向狐f(v, u) = f(v, u) - cf(p) (后向狐往复上述2与3步骤,直至无法找到路径p为止... Read More

[LeetCode 889]Construct Binary Tree from Preorder and Postorder Traversal

原题Return any binary tree that matches the given preorder and postorder traversals.Values in the traversalspreandpostare distinctpositive integers.Example 1Input: pre = [1,2,4,5,3,6,7], post = [4,5,2,6,7,3,1Output: [1,2,3,4,5,6,7Note1 <= pre.length == post.length <= 30pre[]andpost[]are both permutations of1, 2, ..., pre.length.It is guaranteed an answer exists. If there exists multiple an... Read More