您的位置:首页 > 游戏 > 游戏 > 怡美工业设计公司_编程免费网站_什么是搜索引擎营销?_邵阳做网站的公司

怡美工业设计公司_编程免费网站_什么是搜索引擎营销?_邵阳做网站的公司

2025/7/15 23:29:56 来源:https://blog.csdn.net/ProgramNovice/article/details/143245537  浏览:    关键词:怡美工业设计公司_编程免费网站_什么是搜索引擎营销?_邵阳做网站的公司
怡美工业设计公司_编程免费网站_什么是搜索引擎营销?_邵阳做网站的公司

LeetCode Hot 100:栈

20. 有效的括号

class Solution {
public:bool isValid(string s) {stack<char> stk;for (char& c : s) {if (c == ')') {if (stk.empty() || stk.top() != '(')return false;elsestk.pop();} else if (c == ']') {if (stk.empty() || stk.top() != '[')return false;elsestk.pop();} else if (c == '}') {if (stk.empty() || stk.top() != '{')return false;elsestk.pop();} elsestk.push(c);}return stk.empty();}
};

155. 最小栈

class MinStack {
private:stack<int> s, minStack;public:MinStack() {}void push(int val) {s.push(val);if (minStack.empty() || val <= minStack.top())minStack.push(val);}void pop() {int x = s.top();s.pop();if (!minStack.empty() && minStack.top() == x)minStack.pop();}int top() { return s.top(); }int getMin() { return minStack.top(); }
};/*** Your MinStack object will be instantiated and called as such:* MinStack* obj = new MinStack();* obj->push(val);* obj->pop();* int param_3 = obj->top();* int param_4 = obj->getMin();*/

394. 字符串解码

class Solution {
public:string decodeString(string s) {int n = s.size();int i = 0;stack<string> stk;while (i < n) {char c = s[i];if (isdigit(c)) {string num;while (isdigit(s[i])) {num.push_back(s[i]);i++;}stk.push(num);} else if (isalpha(c) || c == '[') {stk.push(string(1, s[i]));i++;} else { // c == ']'vector<string> sub;while (stk.top() != "[") {sub.insert(sub.begin(), stk.top());stk.pop();}stk.pop(); // 左括号出栈// 此时栈顶为当前 sub 对应的字符串应该出现的次数int repeatTime = stoi(stk.top());stk.pop();string temp;while (repeatTime--) {for (string& str : sub)temp += str;}stk.push(temp);i++;}}string ans;while (!stk.empty()) {ans.insert(0, stk.top());stk.pop();}return ans;}
};

739. 每日温度

思路 1:单调栈

class Solution {
public:vector<int> dailyTemperatures(vector<int>& temperatures) {int n = temperatures.size();vector<int> ans(n, 0);stack<int> s;for (int i = 0; i < n; i++) {int cur = temperatures[i];while (!s.empty() && cur > temperatures[s.top()]) {int j = s.top();s.pop();ans[j] = i - j;}s.push(i);}return ans;}
};

84. 柱状图中最大的矩形

class Solution {
public:int largestRectangleArea(vector<int>& heights) {int n = heights.size();// left[i] = 在 i 左侧的小于 heights[i] 的最近元素的下标vector<int> left(n, -1);stack<int> s;for (int i = 0; i < n; i++) {while (!s.empty() && heights[i] <= heights[s.top()])s.pop();if (!s.empty())left[i] = s.top();s.push(i);}// left[i] = 在 i 右侧的小于 heights[i] 的最近元素的下标vector<int> right(n, n);s = stack<int>();for (int i = n - 1; i >= 0; i--) {while (!s.empty() && heights[i] <= heights[s.top()])s.pop();if (!s.empty())right[i] = s.top();s.push(i);}int ans = INT_MIN;for (int i = 0; i < n; i++) {int len = right[i] - left[i] - 1;ans = max(ans, heights[i] * len);}return ans;}
};

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com