摘要: 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)