01用两个栈组成一个队列
//2024.2.7
#include <iostream>
#include <stack>
using namespace std;
class Myqueue{
public:
Myqueue(){}
~Myqueue(){}
void push(int x){
stacki.push(x);
}
int pop(){
if(stacko.empty()){
while(!stacki.empty()){
stacko.push(stacki.top());
stacki.pop();
}
}
int result = stacko.top();
stacko.pop();
return result;
}
int peep(){
int res = this->pop();
stacko.push(res);
return res;
}
bool empty(){
return stacki.empty() && stacko.empty();
}
private:
stack<int> stacki;
stack<int> stacko;
};
int main(){
Myqueue queue;
queue.push(1);
queue.push(2);
queue.push(3);
for(int i = 0; i < 3; i++){
cout << queue.pop() << " ";
}
}
02用两个队列组成一个栈
//2024.2.7
#include <iostream>
#include <queue>
using namespace std;
class Mystack{
public:
Mystack(){}
~Mystack(){}
void push(int x){
queue1.push(x);
}
int pop(){
if(queue1.empty()){
cout << "no data" << endl;
return;
}
int size = queue1.size();
while(size > 1){
queue2.push(queue1.front());
queue1.pop();
size--;
}
int result = queue1.front();
queue1.pop();
queue1 = queue2;
while(!queue2.empty()){
queue2.pop();
}
return result;
}
bool empty(){
return queue1.empty();
}
int top(){
return queue1.back();
}
private:
queue<int> queue1;
queue<int> queue2;
};
int main(){
Mystack stack;
stack.push(1);
stack.push(2);
stack.push(3);
for(int i = 0; i < 3; i++){
cout << stack.pop() << " ";
}
}
03用一个队列实现一个栈
#include <iostream>
#include <queue>
using namespace std;
class Mystack{
public:
Mystack(){}
~Mystack(){}
void push(int x){
queue.push(x);
}
int pop(){
if(queue.empty()){
cout << "no data" << endl;
return -1;
}
int size = queue.size();
while(size > 1){
queue.push(queue.front());
queue.pop();
size--;
}
int result = queue.front();
queue.pop();
return result;
}
bool empty(){
return queue.empty();
}
int top(){
return queue.back();
}
private:
queue<int> queue;
};
int main(){
Mystack stack;
stack.push(1);
stack.push(2);
stack.push(3);
for(int i = 0; i < 3; i++){
cout << stack.pop() << " ";
}
}
04匹配括号
//2024.2.8
#include <iostream>
#include <string>
#include <stack>
using namespace std;
bool a(string& s){
stack<char> stack;
for(int i = 0; i < s.size(); i++){
if(s[i] == '{' || s[i] == '[' || s[i] == '('){
stack.push(s[i]);
}else if(s[i] == '}'){
if(!stack.empty() && stack.top() == '{'){
stack.pop();
}else{
return false;
}
}else if(s[i] == ']'){
if(!stack.empty() && stack.top() == '['){
stack.pop();
}else{
return false;
}
}else if(s[i] == ')'){
if(!stack.empty() && stack.top() == '('){
stack.pop();
}else{
return false;
}
}
}
return stack.empty();
}
int main(){
string s = "{[(a+b)+(c+d)]*e}*f";
cout << a(s) ? "匹配" : "不匹配";
}
05逆波兰表达式
//2024.2.8
#include <iostream>
#include <vector>
#include <string>
#include <stack>
using namespace std;
int a(vector<string>& nums){
stack<int> stack;
for(int i = 0; i < nums.size(); i++){
if(nums[i] != "+" && nums[i] != "-" && nums[i] != "*" && nums[i] != "/"){
stack.push(stoi(nums[i]));
}else{
int num2 = stack.top();
stack.pop();
int num1 = stack.top();
stack.pop();
if(nums[i] == "+"){
stack.push(num1 + num2);
}else if(nums[i] == "-"){
stack.push(num1 - num2);
}else if(nums[i] == "*"){
stack.push(num1 * num2);
}else if(nums[i] == "/"){
stack.push(num1 / num2);
}
}
}
int result = stack.top();
stack.pop();
return result;
}
int main(){
vector<string> nums = {"2","3","+","8","*","5","/","7","-"};
cout << a(nums);
}
06滑动窗口最大值
//2024.2.8
#include <iostream>
#include <algorithm>
#include <vector>
#include <deque>
using namespace std;
void print(int num){
cout << num << " ";
}
//双指针
vector<int> a(vector<int> nums, int k){
vector<int> result;
int slow = 0, fast = k - 1;
int max = nums[0];
for(; fast < nums.size(); fast++, slow++){
for(int i = slow; i <= fast; i++){
if(nums[i] > max){
max = nums[i];
}
}
result.push_back(max);
max = nums[fast];
}
return result;
}
//单调队列
class MyQuque{
public:
deque<int> que;
void pop(int value){
if(!que.empty() && que.front() == value){
que.pop_front();
}
}
void push(int value){
while(!que.empty() && value > que.back()){
que.pop_back();
}
que.push_back(value);
}
int front(){
return que.front();
}
};
vector<int> b(vector<int> nums, int k){
MyQuque que;
vector<int> result;
for(int i = 0; i < k; i++){
que.push(nums[i]);
}
result.push_back(que.front());
for(int i = k; i < nums.size(); i++){
que.pop(nums[i - k]);
que.push(nums[i]);
result.push_back(que.front());
}
return result;
}
int main(){
vector<int> nums = {3,4,2,5,1,6,10,8,12};
vector<int> result = b(nums, 3);
for_each(result.begin(), result.end(), print);
}
07前k个高频元素
//2024.2.18
#include <iostream>
#include <vector>
#include <unordered_map>
#include <queue>
#include <algorithm>
using namespace std;
//小顶堆
class mycomparison{
public:
bool operator()(const pair<int, int>& lhs, const pair<int, int>& rhs){
return lhs.second > rhs.second;
}
};
vector<int> topK(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(auto 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 = 0; i < k; i++){
result[i] = pri_que.top().first;
pri_que.pop();
}
return result;
}
void print(int value){
cout << value << " ";
}
int main(){
vector<int> a = {3,3,3,4,4,5,1,1,2,2,2,3,5,5,5};
vector<int> result = topK(a, 2);
for_each(result.begin(), result.end(), print);
}
08接雨水
//2024.2.18
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
//双指针
int trap1(vector<int>& height){
if(height.size() < 3){
return 0;
}
int result = 0;
for(int i = 1; i < height.size() - 1; i++){
int left = 0, right = 0;
for(int l = 0; l < i; l++){
left = max(left, height[l]);
}
for(int r = i + 1; r < height.size(); r++){
right = max(right, height[r]);
}
left = min(left, right);
if(left > height[i]){
result += left - height[i];
}
}
return result;
}
//动态规划
int trap2(vector<int>& height){
int result = 0;
vector<int> maxleft(height.size(), 0);
vector<int> maxright(height.size(), 0);
maxleft[0] = height[0];
for(int i = 1; i < height.size() - 1; i++){
maxleft[i] = max(maxleft[i -1], height[i]);
}
maxright[height.size() - 1] = height[height.size() - 1];
for(int i = height.size() - 2; i > 0; i--){
maxright[i] = max(maxright[i + 1], height[i]);
}
int smaller = 0;
for(int i = 1; i < height.size() - 1; i++){
smaller = min(maxleft[i], maxright[i]);
if(smaller > height[i]){
result += smaller - height[i];
}
}
return result;
}
//单调栈
int trap3(vector<int>& height){
int result = 0;
return result;
}
int main(){
vector<int> height = {1,0,2,1,3,1,0,1,2,0,1};
cout << trap2(height);
}