前言
一个本硕双非的小菜鸡,备战24年秋招,计划刷完卡子哥的刷题计划,加油!
推荐一手卡子哥的刷题网站,感谢卡子哥。代码随想录
一、232. 用栈实现队列
Note:模拟就完了,开始的基础题
class MyQueue {
public:
stack<int> stIn;
stack<int> stOut;
MyQueue() {
}
void push(int x) {
stIn.push(x);
}
int pop() {
if (stOut.empty()) {
while (!stIn.empty()) {
stOut.push(stIn.top());
stIn.pop();
}
}
int result = stOut.top();
stOut.pop();
return result;
}
int peek() {
int res = this->pop();
stOut.push(res);
return res;
}
bool empty() {
return stIn.empty() && stOut.empty();
}
};
/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue* obj = new MyQueue();
* obj->push(x);
* int param_2 = obj->pop();
* int param_3 = obj->peek();
* bool param_4 = obj->empty();
*/
二、225. 用队列实现栈
Note:模拟就完了,开始的基础题
class MyStack {
public:
queue<int> que1;
queue<int> que2;
MyStack() {
}
void push(int x) {
que1.push(x);
}
int pop() {
int m = que1.size();
m--;
while (m--) {
que2.push(que1.front());
que1.pop();
}
int result = que1.front();
que1.pop();
que1 = que2;
while (!que2.empty()) {
que2.pop();
}
return result;
}
int top() {
return que1.back();
}
bool empty() {
return que1.empty();
}
};
/**
* Your MyStack object will be instantiated and called as such:
* MyStack* obj = new MyStack();
* obj->push(x);
* int param_2 = obj->pop();
* int param_3 = obj->top();
* bool param_4 = obj->empty();
*/
三、20. 有效的括号
Note:用一个栈,正好匹配到就削掉
class Solution {
public:
bool isValid(string s) {
if (s.size() % 2 != 0) return false;
stack<char> res;
for (int i = 0; i < s.size(); i++) {
if (s[i] == '(') res.push(')');
else if (s[i] == '[') res.push(']');
else if (s[i] == '{') res.push('}');
else if (res.empty() || res.top() != s[i]) return false;
else res.pop();
}
return res.empty();
}
};
四、1047. 删除字符串中的所有相邻重复项
Note:用栈来模拟
class Solution {
public:
string removeDuplicates(string s) {
stack<char> st;
for (char a : s) {
if (st.empty() || a != st.top()) {
st.push(a);
} else {
st.pop();
}
}
string res = "";
while (!st.empty()) {
res += st.top();
st.pop();
}
reverse(res.begin(), res.end());
return res;
}
};
五、150. 逆波兰表达式求值
Note:相当于是二叉树中的后序遍历,可以理解为对每一个相邻元素做运算
class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<int> stk;
int n = tokens.size();
for(int i = 0; i < n; i++)
{
string& token = tokens[i];
if(token != "+" && token != "-" && token != "*" && token != "/")
{
stk.push(atoi(token.c_str()));
}
else
{
int num2 = stk.top();
stk.pop();
int num1 = stk.top();
stk.pop();
switch(token[0])
{
case '+':
stk.push(num1 + num2);
break;
case '-':
stk.push(num1 - num2);
break;
case '*':
stk.push(num1 * num2);
break;
case '/':
stk.push(num1 / num2);
break;
}
}
}
return stk.top();
}
};
六、347. 前 K 个高频元素
Note:优先队列的解题方法,总有一天我也可以写出这样的代码的,加油!
class Solution {
public:
class mycomparison {
public:
bool operator()(const pair<int, int>& lhs, const pair<int, int>& rhs) {
return lhs.second > rhs.second;
}
};
vector<int> topKFrequent(vector<int>& nums, int k) {
/*要统计元素出现频率*/
unordered_map<int, int> map;
for (int i = 0; i < nums.size(); i++) {
map[nums[i]]++;
}
priority_queue<pair<int, int>, vector<pair<int, int>>, mycomparison> pri_que;
for (unordered_map<int, int>::iterator it = map.begin(); it != map.end(); it++) {
pri_que.push(*it);
if (pri_que.size() > k) {
pri_que.pop();
}
}
vector<int> result(k);
for (int i = k - 1; i >= 0; i--) {
result[i] = pri_que.top().first;
pri_que.pop();
}
return result;
}
};
总结
栈和队列我之前还不以为意,觉得这俩定义还是蛮简单的,但是就这一个第六题成功让我明白了人外有人天外有天,还得继续努力啊,刚把爹!