算法训练营day10 栈与队列(栈实现队列,队列实现栈,栈的应用)
创始人
2025-01-09 07:03:57
0

💡 解题思路

  1. 📝 确定输入与输出
  2. 🔍 分析复杂度
  3. 🔨 复杂题目拆分严谨且完整 地拆分为更小的可以解决的子问题(栈和队列的功能,栈和队列的应用场景)–(多总结
  4. 💭 选择处理逻辑: 根据拆分后的子问题,总结并选择合适的问题处理思路栈和队列的转换逻辑
  5. 🔎 检查特殊情况:边界条件和特殊情况
  6. 🏁 返回结果

232.用栈实现队列

    class MyQueue {         private Stack inStack;         private Stack outStack;          public MyQueue() {             inStack = new Stack<>();             outStack = new Stack<>();         }          public void push(int x) {             inStack.push(x);         }          public int pop() {             moveInToOutStack();             return outStack.pop();         }          public int peek() {             moveInToOutStack();             return outStack.peek();         }          public boolean empty() {             return inStack.isEmpty() && outStack.isEmpty();         }                  private void moveInToOutStack() {             if (outStack.isEmpty()) {                 while (!inStack.isEmpty()) {                     outStack.push(inStack.pop());                 }             }         }     } 

225. 用队列实现栈

class MyStack {      private Queue mainQueue;     private Queue auxiliaryQueue;      public MyStack() {         mainQueue = new LinkedList<>();         auxiliaryQueue = new LinkedList<>();     }      public void push(int x) {         mainQueue.offer(x);     }      public int pop() {         while(mainQueue.size() > 1) {             auxiliaryQueue.offer(mainQueue.poll());         }         int num = mainQueue.poll();         Queue temp = mainQueue;         mainQueue = auxiliaryQueue;         auxiliaryQueue = temp;         return num;     }      public int top() {         while(mainQueue.size() > 1) {             auxiliaryQueue.offer(mainQueue.poll());         }         int num = mainQueue.poll();         auxiliaryQueue.offer(num);         Queue temp = mainQueue;         mainQueue = auxiliaryQueue;         auxiliaryQueue = temp;         return num;     }      public boolean empty() {         return mainQueue.isEmpty();     } } 

20. 有效的括号

class Solution {     public static boolean isValid(String s) {         HashMap map = new HashMap<>();         map.put(')','(');         map.put('}','{');         map.put(']','[');         Stack queueStack = new Stack<>();         int len = s.length();         if (len % 2 != 0) return false;         for (int i = 0; i < len; i++) {             char ch = s.charAt(i);             if (!map.containsKey(ch)) queueStack.push(ch);             else {                 if (queueStack.isEmpty() || queueStack.pop() != map.get(ch)) return false;             }         }         return queueStack.isEmpty();     } } 

1047. 删除字符串中的所有相邻重复项 (可以用栈,下面用的双指针)

class Solution {     public static String removeDuplicates(String s) {         int j = -1;         int len = s.length();         char[] chars = s.toCharArray();         for (int i = 0; i < len; i++) {             if (j >= 0 && chars[i] == chars[j]) {                 j--;             } else {                 j++;                 chars[j] = chars[i];             }         }         return String.copyValueOf(chars, 0, j+1);     } } 

相关内容

热门资讯

wepower有外挂!hhpo... wepower有外挂!hhpoker德州有挂(透视)存在挂教程(有挂猫腻)-哔哩哔哩;1、超多福利:...
wepokeai机器人!wej... wepokeai机器人!wejoker透视方法(透视)细节揭秘(确实有挂)-哔哩哔哩;亲真的是有正版...
wepokeai代打的胜率!p... wepokeai代打的胜率!pokermaster脚本(透视)黑科技教程(有挂秘笈)-哔哩哔哩是一款...
wepoke黑科技!德普之星透... wepoke黑科技!德普之星透视辅助软件是真的(透视)教你攻略(有挂方略)-哔哩哔哩是一款可以让一直...
wepoke是真的有挂!hh ... wepoke是真的有挂!hh poker辅助有用(透视)细节方法(有挂功能)-哔哩哔哩;最新版202...
wepokeai代打逻辑!po... wepokeai代打逻辑!pokemmo手机版脚本免费(透视)透明教程(真的有挂)-哔哩哔哩;是一款...
wepoke有app软件!we... wepoke有app软件!wepoker辅助工具(透视)透牌教程(有挂分析)-哔哩哔哩;wepoke...
wepok软件透明挂!哈糖大菠... 【福星临门,好运相随】;wepok软件透明挂!哈糖大菠萝软件下载(透视)2025新版教程(揭秘有挂)...
七分钟了解!新畅游互娱科技(辅... 七分钟了解!新畅游互娱科技(辅助挂)详细透视开挂辅助新2025版(切实真的有挂)-哔哩哔哩;1、首先...
wepokeai代打逻辑!德州... wepokeai代打逻辑!德州hhpoker脚本(透视)辅助教程(有挂秘笈)-哔哩哔哩;是一款可以让...