class Solution {
public:
bool isValid(string s) {
stack<char> st;
for (char e : s)
{
if (st.empty()) st.push(e);
else if (st.top() == '(' && e == ')' || st.top() == '{' && e == '}'
|| st.top() == '[' && e == ']') st.pop();
else st.push(e);
}
return st.empty();
}
};
class MinStack {
stack<int> st;
stack<int> minst;
public:
MinStack() {
}
void push(int val) {
st.push(val);
if (minst.empty() || val <= minst.top())
{
minst.push(val);
}
}
void pop() {
if (st.top() == minst.top())
{
minst.pop();
}
st.pop();
}
int top() {
return st.top();
}
int getMin() {
return minst.top();
}
};
class Solution {
public:
string decodeString(string s) {
int i = 0;
return dfs(s, i);
}
string dfs(const string& s, int& i)
{
string str;
while (i < s.size() && s[i] != ']')
{
if (isdigit(s[i]))
{
int num = 0;
while (i < s.size() && s[i] != '[')
{
num = num * 10 + s[i++] - '0';
}
i++; // 跳过'['
string tmp = dfs(s, i);
i++; // 跳过']'
while (num--) str += tmp;
}
else str += s[i++];
}
return str;
}
};
class Solution {
public:
vector<int> dailyTemperatures(vector<int>& temperatures) {
int n = temperatures.size();
stack<int> st;
vector<int> res(n);
for (int i = 0; i < n; i++)
{
while (st.size() && temperatures[st.top()] < temperatures[i])
{
int t = st.top();
st.pop();
res[t] = i - t;
}
st.push(i);
}
return res;
}
};
单调栈。
class Solution {
public:
int largestRectangleArea(vector<int>& heights) {
int n = heights.size();
vector<int> left(n), right(n);
stack<int> st;
for (int i = 0; i < n; i++)
{
while (st.size() && heights[st.top()] >= heights[i])
{
st.pop();
}
left[i] = st.empty() ? -1 : st.top();
st.push(i);
}
st = stack<int>();
for (int i = n - 1; i >= 0; i--)
{
while (st.size() && heights[st.top()] >= heights[i])
{
st.pop();
}
right[i] = st.empty() ? n : st.top();
st.push(i);
}
int res = 0;
for (int i = 0; i < n; i++)
{
res = max(res, (right[i] - left[i] - 1) * heights[i]);
}
return res;
}
};
优化。
class Solution {
public:
int largestRectangleArea(vector<int>& heights) {
int n = heights.size();
vector<int> left(n, -1), right(n, n);
stack<int> st;
for (int i = 0; i < n; i++)
{
while (st.size() && heights[st.top()] > heights[i])
{
right[st.top()] = i;
st.pop();
}
if (st.size()) left[i] = st.top();
st.push(i);
}
int res = 0;
for (int i = 0; i < n; i++)
{
res = max(res, (right[i] - left[i] - 1) * heights[i]);
}
return res;
}
};
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
int n = nums.size();
for (int i = n - 2 / 2; i >= 0; i--)
{
adjust_down(nums, i, n);
}
while (--k)
{
swap(nums[0], nums[--n]);
adjust_down(nums, 0, n);
}
return nums[0];
}
void adjust_down(vector<int>& nums, int parent, int n)
{
int child = 2 * parent + 1;
while (child < n)
{
if (child + 1 < n && nums[child + 1] > nums[child]) child++;
if (nums[child] > nums[parent])
{
swap(nums[child], nums[parent]);
parent = child;
child = 2 * parent + 1;
}
else break;
}
}
};
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有