摘要: leetcode acwing 单调栈 \(O(n)\) 维护一个严格单调递减的单调栈,对于单调栈的栈顶元素来说,待加入的元素假如比它大,那么对于它就是最近的比它大的元素。不要光从待加入元素作为考虑问题的主体。栈顶元素也是一个考虑角度。另外,单调栈一般是存放下标。 class Solution { 阅读全文
posted @ 2021-01-07 14:02 alexemey 阅读(37) 评论(0) 推荐(0)
摘要: leetcode acwing 递归 没什么好说的,看代码吧 class Solution { public: // 递归 string decodeString(string s) { string res; for (int i = 0; i < s.size();) { // 前面的字母直接加 阅读全文
posted @ 2021-01-07 13:27 alexemey 阅读(33) 评论(0) 推荐(0)
摘要: 单调栈 再加一个单调栈来维护目前的最小值 C++ 代码 class MinStack { public: /** initialize your data structure here. */ stack<int> st; stack<int> minist; MinStack() { } void 阅读全文
posted @ 2021-01-07 10:33 alexemey 阅读(48) 评论(0) 推荐(0)
摘要: leetcode acwing 递归 \(O(n)\) 递归写法是最简单的写法,先遍历左子树,再遍历自己,再遍历右子树。 class Solution { public: vector<int> inorderTraversal(TreeNode* root) { vector<int> res; 阅读全文
posted @ 2021-01-07 09:54 alexemey 阅读(38) 评论(0) 推荐(0)
摘要: leetcode acwing 动态规划 \(O(n^3)\) 想起了矩阵连乘 C++ 代码 class Solution { public: int maxCoins(vector<int>& nums) { int n = nums.size(); vector<int> a(n + 2, 1) 阅读全文
posted @ 2021-01-05 11:44 alexemey 阅读(79) 评论(0) 推荐(0)
摘要: leetcode acwing 动态规划 \(O(n)\) 和之前做的完全平方数比较像 C++ 代码 class Solution { public: int coinChange(vector<int>& coins, int amount) { if (coins.empty()) return 阅读全文
posted @ 2021-01-05 11:10 alexemey 阅读(42) 评论(0) 推荐(0)
摘要: leetcode acwing 动态规划 \(O(3n)\) 状态机DP,第一次碰见 C++ 代码 class Solution { public: int maxProfit(vector<int>& prices) { if (prices.empty()) return 0; int n = 阅读全文
posted @ 2021-01-05 10:49 alexemey 阅读(41) 评论(0) 推荐(0)
摘要: leetcode acwing 动态规划 \(O(n\sqrt{n})\) C++ 代码 class Solution { public: int numSquares(int n) { vector<int> f(n + 1, n); f[0] = 0; for (int i = 1; i <= 阅读全文
posted @ 2021-01-05 10:14 alexemey 阅读(39) 评论(0) 推荐(0)
摘要: leetcode acwing 动态规划 \(O(n)\) C++ 代码 class Solution { public: int maximalSquare(vector<vector<char>>& matrix) { int n = matrix.size(); int m = 0; if ( 阅读全文
posted @ 2021-01-05 09:42 alexemey 阅读(65) 评论(0) 推荐(0)
摘要: Acwing 哈夫曼树 哈夫曼树的编码,可以通过优先级队列来实现。 #include<iostream> #include<algorithm> #include<queue> using namespace std; const int N = 1e5 + 10; // 通过优先级队列来实现自动排 阅读全文
posted @ 2021-01-04 23:36 alexemey 阅读(47) 评论(0) 推荐(0)