动态规划常见问题

优化子问题重复计算 - 斐波那契数列 LeetCode: https://leetcode.cn/problems/fibonacci-number 思路 状态转移方程: \begin{equation} f(n) = \left\{ \begin{array}{ll} 1, & \textrm{n = 1, 2} \\ f(n - 1) + f(n - 2), & \textrm{n > 2} \end{array} \right. \end{equation} 如果暴力计算,有大量的重复计算,比

二叉树常用算法

求树的深度 LeetCode: https://leetcode.cn/problems/maximum-depth-of-binary-tree/ 思路 二叉树问题的求解主要有两种思想: 自顶向下,遍历求解,每向下一层深度加 1,遍历完整颗树后就能找出答案 自底向上,分解子问题,先

数组常用算法

移除有序数组的重复元素 LeetCode: https://leetcode.cn/problems/remove-duplicates-from-sorted-array/ 思路 利用快慢指针技巧 代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 class Solution { public: int removeDuplicates(vector<int>& nums) { int slow = 0; int fast = 1; while (fast < nums.size()) { if (nums[slow] != nums[fast]) { slow++; if

C++ 编程技巧

Assert 的使用 Asser 中的表达式的原则: 肯定声明,不要加否定 没有副作用,不会改变任何数据 例如: 1 2 3 assert(obj.isValid()) assert(state == StateOpening) Assert 是给开发者看的,所以 Assert 只能在 debug 构建中存在,

C++ 常用资源

库 Boost.Spirit X3 Spirit 是一个面向对象,递归下降解析器,它可以直接在 C++ 代码中用类似 EBNF 的格式写语法。 文档:https://www.boost.org/doc/