12.1 232. 用栈实现队列

  1. class MyQueue {
  2. public:
  3. //用两个栈模拟,一个存push的,一个将pop出来的存起来
  4. stack<int> stIn;
  5. stack<int> stOut;
  6. MyQueue() {
  7. }
  8. void push(int x) {
  9. stIn.push(x);
  10. }
  11. int pop() {
  12. if(stOut.empty()){
  13. while(!stIn.empty()){
  14. int x=stIn.top();
  15. stOut.push(x);
  16. stIn.pop();
  17. }
  18. }
  19. int x=stOut.top();
  20. stOut.pop();
  21. return x;
  22. }
  23. int peek() {
  24. int x=this->pop();
  25. stOut.push(x);
  26. return x;
  27. }
  28. bool empty() {
  29. return stIn.empty()&&stOut.empty();
  30. }
  31. };
  32. /**
  33. * Your MyQueue object will be instantiated and called as such:
  34. * MyQueue* obj = new MyQueue();
  35. * obj->push(x);
  36. * int param_2 = obj->pop();
  37. * int param_3 = obj->peek();
  38. * bool param_4 = obj->empty();
  39. */

12.2 225. 用队列实现栈

  1. class MyStack {
  2. public:
  3. queue<int> que;
  4. MyStack() {
  5. }
  6. void push(int x) {
  7. que.push(x);
  8. }
  9. int pop() {
  10. int size=que.size();
  11. size--;
  12. while(size--){
  13. int x=que.front();
  14. que.push(x);
  15. que.pop();
  16. }
  17. int x=que.front();
  18. que.pop();
  19. return x;
  20. }
  21. int top() {
  22. return que.back();
  23. }
  24. bool empty() {
  25. return que.empty();
  26. }
  27. };
  28. /**
  29. * Your MyStack object will be instantiated and called as such:
  30. * MyStack* obj = new MyStack();
  31. * obj->push(x);
  32. * int param_2 = obj->pop();
  33. * int param_3 = obj->top();
  34. * bool param_4 = obj->empty();
  35. */

12.3 20. 有效的括号

  1. class Solution {
  2. public:
  3. bool isValid(string s) {
  4. stack<int> st;
  5. for(int i=0;i<s.length();i++){
  6. if(s[i]=='('){
  7. st.push(')');
  8. }else if(s[i]=='['){
  9. st.push(']');
  10. }else if(s[i]=='{'){
  11. st.push('}');
  12. }else if(!st.empty()&&st.top()==s[i]){
  13. st.pop();
  14. }else{
  15. return false;
  16. }
  17. }
  18. return st.empty();
  19. }
  20. };

12.4 921. 使括号有效的最少添加

  1. class Solution {
  2. public:
  3. int minAddToMakeValid(string s) {
  4. int need=0;//右括号的需求
  5. int needLeft=0;//左括号的需求
  6. for(int i=0;i<s.length();i++){
  7. if(s[i]=='('){
  8. need++;
  9. }else if(s[i]==')'){
  10. need--;
  11. if(need==-1){
  12. need=0;
  13. needLeft++;
  14. }
  15. }
  16. }
  17. return need+needLeft;
  18. }
  19. };

12.5 1541. 平衡括号字符串的最少插入次数

  1. class Solution {
  2. public:
  3. int minInsertions(string s) {
  4. int needRight=0,result=0;
  5. for(int i=0;i<s.size();i++){
  6. if(s[i]=='('){
  7. needRight+=2;
  8. if(needRight%2==1){
  9. //奇数时,要插入一个右括号
  10. result++;
  11. //右括号需求-1
  12. needRight--;
  13. }
  14. }else if(s[i]==')'){
  15. needRight--;
  16. if(needRight==-1){
  17. needRight=1;
  18. result++;
  19. }
  20. }
  21. }
  22. return needRight+result;
  23. }
  24. };

12.6 1047. 删除字符串中的所有相邻重复项

  1. //栈
  2. class Solution {
  3. public:
  4. string removeDuplicates(string s) {
  5. stack<int> st;
  6. for(int i=0;i<s.length();i++){
  7. if(st.empty()||s[i]!=st.top()){
  8. st.push(s[i]);
  9. }else{
  10. st.pop();
  11. }
  12. }
  13. string result="";
  14. while(!st.empty()){
  15. result+=st.top();
  16. st.pop();
  17. }
  18. reverse(result.begin(),result.end());
  19. return result;
  20. }
  21. };
  22. //字符串
  23. class Solution {
  24. public:
  25. string removeDuplicates(string s) {
  26. string result="";
  27. for(int i=0;i<s.length();i++){
  28. if(result.empty()||s[i]!=result.back()){
  29. result.push_back(s[i]);
  30. }else{
  31. result.pop_back();
  32. }
  33. }
  34. return result;
  35. }
  36. };

12.7 150. 逆波兰表达式求值

  1. class Solution {
  2. public:
  3. int evalRPN(vector<string>& tokens) {
  4. stack<int> st;
  5. for(int i=0;i<tokens.size();i++){
  6. if(tokens[i]=="+"||tokens[i]=="-"||tokens[i]=="*"||tokens[i]=="/"){
  7. int num2=st.top();
  8. st.pop();
  9. int num1=st.top();
  10. st.pop();
  11. if(tokens[i]=="+") st.push(num1+num2);
  12. else if(tokens[i]=="-") st.push(num1-num2);
  13. else if(tokens[i]=="*") st.push(num1*num2);
  14. else if(tokens[i]=="/") st.push(num1/num2);
  15. }else{
  16. st.push(stoi(tokens[i]));
  17. }
  18. }
  19. return st.top();
  20. }
  21. };

12.8 239. 滑动窗口最大值

  1. class Solution {
  2. private:
  3. class MyQueue{
  4. //保证队列第一个元素为最大的,单调队列
  5. public:
  6. deque<int> d;
  7. void push(int value){
  8. //比自己小的元素都删去
  9. while(!d.empty()&&value>d.back()){
  10. d.pop_back();
  11. }
  12. d.push_back(value);
  13. }
  14. void pop(int value){
  15. if(!d.empty()&&value==d.front()){
  16. d.pop_front();
  17. }
  18. }
  19. int getMax(){
  20. return d.front();
  21. }
  22. };
  23. public:
  24. vector<int> maxSlidingWindow(vector<int>& nums, int k) {
  25. MyQueue mq;
  26. vector<int> result;
  27. for(int i=0;i<k;i++){
  28. mq.push(nums[i]);
  29. }
  30. result.push_back(mq.getMax());
  31. for(int i=k;i<nums.size();i++){
  32. mq.pop(nums[i-k]);
  33. mq.push(nums[i]);
  34. result.push_back(mq.getMax());
  35. }
  36. return result;
  37. }
  38. };

12.9 347. 前 K 个高频元素

  1. class Solution {
  2. public:
  3. class myComparison{
  4. public:
  5. bool operator()(const pair<int,int>& a,const pair<int,int>& b){
  6. return a.second>b.second;
  7. }
  8. };
  9. vector<int> topKFrequent(vector<int>& nums, int k) {
  10. //map的key为num,value为freq
  11. unordered_map<int,int> map;
  12. for(int i=0;i<nums.size();i++){
  13. map[nums[i]]++;
  14. }
  15. //根据map的freq进行排序,小堆树
  16. //定义 priority_queue<数据类型、底层容器、比较函数>
  17. priority_queue<pair<int,int>,vector<pair<int,int>>,myComparison> pri_que;
  18. for(auto it=map.begin();it!=map.end();it++){
  19. pri_que.push(*it);
  20. if(pri_que.size()>k){
  21. pri_que.pop(); //满k个后弹出freq低的
  22. }
  23. }
  24. //输出priority_queue剩下的k个
  25. vector<int> result(k);
  26. for(int i=k-1;i>=0;i--){
  27. result[i]=pri_que.top().first;
  28. pri_que.pop();
  29. }
  30. return result;
  31. }
  32. };

12.10 496. 下一个更大元素 I

  1. class Solution {
  2. public:
  3. vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
  4. stack<int> st;
  5. unordered_map<int,int> map;
  6. //找num2中每个元素右边第一大的数 存入hash_table
  7. for(int i=nums2.size()-1;i>=0;i--){
  8. while(!st.empty()&&nums2[i]>=st.top()){
  9. st.pop();
  10. }
  11. //先取出最大的数,在存入本数
  12. map[nums2[i]]=st.empty()?-1:st.top();
  13. st.push(nums2[i]);
  14. }
  15. //nums1中的数,在hash_table中查找
  16. vector<int> result(nums1.size());
  17. for(int i=0;i<nums1.size();i++){
  18. result[i]=map[nums1[i]];
  19. }
  20. return result;
  21. }
  22. };

12.11 503. 下一个更大元素 II

  1. class Solution {
  2. public:
  3. vector<int> nextGreaterElements(vector<int>& nums) {
  4. int n=nums.size();
  5. stack<int> st;
  6. vector<int> result(n);
  7. for(int i=2*n-1;i>=0;i--){
  8. while(!st.empty()&&nums[i%n]>=st.top()){
  9. st.pop();
  10. }
  11. result[i%n]=st.empty()?-1:st.top();
  12. st.push(nums[i%n]);
  13. }
  14. return result;
  15. }
  16. };

12.12 739. 每日温度

  1. class Solution {
  2. public:
  3. vector<int> dailyTemperatures(vector<int>& temperatures) {
  4. int n=temperatures.size();
  5. stack<int> st;
  6. vector<int> result(n);
  7. for(int i=n-1;i>=0;i--){
  8. while(!st.empty()&&temperatures[i]>=temperatures[st.top()]){
  9. st.pop();
  10. }
  11. result[i]=st.empty()?0:(st.top()-i);
  12. st.push(i);
  13. }
  14. return result;
  15. }
  16. };

12.13 316. 去除重复字母

  1. class Solution {
  2. public:
  3. string removeDuplicateLetters(string s) {
  4. stack<char> st;
  5. unordered_set<char> inStack; //记录元素是否出现
  6. unordered_map<char,int> count; //记录出现次数
  7. for(char c:s){
  8. count[c]++;
  9. }
  10. for(char c:s){
  11. count[c]--;
  12. //如果没有出现记录
  13. if(inStack.find(c)==inStack.end()){
  14. //比较该元素与栈顶元素的次序,判断是否需要弹出
  15. while(!st.empty()&&st.top()>c&&count[st.top()]!=0){
  16. //如果之后不在出现该元素,不弹出,还出现的话,就弹出
  17. inStack.erase(st.top()); //删除出现记录
  18. st.pop();
  19. }
  20. inStack.insert(c); //增加出现记录
  21. st.push(c);
  22. }
  23. }
  24. string result="";
  25. while(!st.empty()){
  26. result.push_back(st.top());
  27. st.pop();
  28. }
  29. reverse(result.begin(),result.end());
  30. return result;
  31. }
  32. };