当前位置:网站首页>411 stack and queue (20. valid parentheses, 1047. delete all adjacent duplicates in the string, 150. inverse Polish expression evaluation, 239. sliding window maximum, 347. the first k high-frequency
411 stack and queue (20. valid parentheses, 1047. delete all adjacent duplicates in the string, 150. inverse Polish expression evaluation, 239. sliding window maximum, 347. the first k high-frequency
2022-06-24 10:02:00 【liufeng2023】
20. Valid parenthesis

class Solution {
public:
bool isValid(string s) {
stack<int> st;
for (int i = 0; i < s.size(); i++)
{
if (s[i] == '(')
{
st.push(')');
}
else if (s[i] == '{')
{
st.push('}');
}
else if(s[i] == '[')
{
st.push(']');
}
else if (st.empty() || st.top() != s[i])
{
return false;
}
else
{
st.pop();
}
}
return st.empty();
}
};

1047. Delete all adjacent duplicates in the string

class Solution {
public:
string removeDuplicates(string s) {
stack<char> st;
for (char s1 : s)
{
if (st.empty() || st.top() != s1)
{
st.push(s1);
}
else
{
st.pop();
}
}
string result = "";
while (!st.empty())
{
result += st.top();
st.pop();
}
reverse(result.begin(), result.end());
return result;
}
};

150. Evaluate the inverse Polish expression

class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<int> st;
for (int i = 0; i < tokens.size(); i++)
{
if (tokens[i] == "+" || tokens[i] == "-" || tokens[i] == "*" || tokens[i] == "/")
{
int num1 = st.top();
st.pop();
int num2 = st.top();
st.pop();
if (tokens[i] == "+") st.push(num2 + num1);
if (tokens[i] == "-") st.push(num2 - num1);
if (tokens[i] == "*") st.push(num2 * num1);
if (tokens[i] == "/") st.push(num2 / num1);
}
else
{
st.push(stoi(tokens[i]));
}
}
int res = st.top();
st.pop();
return res;
}
};

239. Maximum sliding window

class Solution {
private:
class MyQueue {
// A monotonous queue ( From big to small )
public:
deque<int> que; // Use deque To implement monotonic queues
// Every time it pops up , Compare whether the current value to pop up is equal to the value of the queue exit element , Pop up if equal .
// meanwhile pop Judge whether the queue is currently empty .
void pop(int value) {
if (!que.empty() && value == que.front()) {
que.pop_front();
}
}
// If push The value of is greater than the value of the entry element , Then pop up the value at the back of the queue , until push The value of is less than or equal to the value of the queue entry element .
// This keeps the values in the queue monotonous, from large to small .
void push(int value) {
while (!que.empty() && value > que.back()) {
que.pop_back();
}
que.push_back(value);
}
// Query the maximum value in the current queue Directly return to the front of the queue, that is front That's all right. .
int front() {
return que.front();
}
};
public:
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
MyQueue que;
vector<int> result;
for (int i = 0; i < k; i++) {
// Before k Put the elements in the queue
que.push(nums[i]);
}
result.push_back(que.front()); // result Before recording k The maximum value of the element
for (int i = k; i < nums.size(); i++) {
que.pop(nums[i - k]); // Slide the window to remove the front most element
que.push(nums[i]); // Add the last element before sliding the window
result.push_back(que.front()); // Record the corresponding maximum value
}
return result;
}
};

347. front K High frequency elements

class Solution {
public:
vector<int> topKFrequent(vector<int>& nums, int k) {
map<int,int> mp;
vector<int> res;
for(int num : nums)
{
mp[num]++;
}
vector<pair<int,int>> vmp;
for(auto it = mp.begin(); it != mp.end(); it++)
{
vmp.push_back({
it->first, it->second});
}
sort(vmp.begin(), vmp.end(),
[](const pair<int, int>& x, const pair<int,int>& y)->int{
return x.second > y.second;});
for(int i = 0; i < k; i++)
{
res.push_back(vmp[i].first);
}
return res;
}
};

边栏推荐
- 队列Queue
- Mise en œuvre du rendu de liste et du rendu conditionnel pour l'apprentissage des applets Wechat.
- ByteDance Interviewer: talk about the principle of audio and video synchronization. Can audio and video be absolutely synchronized?
- Nlp-d59-nlp game D28 - I think it's OK - stage summary - mentality adjustment
- 英伟达这篇CVPR 2022 Oral火了!2D图像秒变逼真3D物体!虚拟爵士乐队来了!
- IDEA 无法保存设置 源根 D:XXXX在模块XXX中重复
- 算法---矩阵中战斗力最弱的 K 行(Kotlin)
- Record the range of data that MySQL update will lock
- Turn to: CEO of Samsung Electronics: all decisions should start from recognizing yourself
- PTA monkey chooses King (Joseph Ring problem)
猜你喜欢
随机推荐
js单例模式
买的长期理财产品,可以转短吗?
Mise en œuvre du rendu de liste et du rendu conditionnel pour l'apprentissage des applets Wechat.
五心红娘
Binary tree part I
414-二叉树的递归遍历
Open Oracle server under Linux to allow remote connection
el-table点击添加行样式
port 22: Connection refused
vim的使用
物联网?快来看 Arduino 上云啦
Thinkphp5 multi language switching project practice
Thinkphp5 clear the cache cache, temp cache and log cache under runtime
Nlp-d59-nlp game D28 - I think it's OK - stage summary - mentality adjustment
el-table表格的拖拽 sortablejs
linux下oracle服务器打开允许远程连接
桌面软件开发框架大赏
队列Queue
5 minutes, excellent customer service chat handling skills
Desktop software development framework reward









