算法训练营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);     } } 

相关内容

热门资讯

2分钟德州透视挂!hhpoke... 2分钟德州透视挂!hhpoker开挂教程,德州透视插件,详细教程(有挂功能)1、完成德州透视插件透视...
九分钟开辅助!wepoker辅... 九分钟开辅助!wepoker辅助软件价格(透视底牌)详细辅助程序(本来是真的有挂)1、任何wepok...
9分钟透视插件!hh poke... 9分钟透视插件!hh poker软件,hhpoker是正品吗,详细教程(有挂智能)1、hh poke...
7分钟插件辅助!wepoker... 7分钟插件辅助!wepoker买钻石有用吗(透视底牌)详细辅助插件(果然是有挂)7分钟插件辅助!we...
八分钟作弊实战!hhpoker... 八分钟作弊实战!hhpoker脚本,hhpoker辅助靠谱吗,详细教程(有挂安装)1、完成hhpok...
二分钟苹果版!wepoker有... 二分钟苹果版!wepoker有辅助器吗(透视底牌)详细辅助免费(切实真的是有挂)1、点击下载安装,插...
八分钟破解工具!德州透视hhp... 八分钟破解工具!德州透视hhpoker,hh poker辅助器先试用,详细教程(有挂插件)1、该软件...
二分钟作弊!wepoker私人... 二分钟作弊!wepoker私人局可以透视(透视底牌)详细辅助作弊器(本来真的有挂);暗藏猫腻,小编详...
五分钟外挂!德扑HHpoker... 五分钟外挂!德扑HHpoker有挂吗(透视脚本)详细辅助助手(确实真的是有挂)1、任何ai辅助神器的...
5分钟新号!wepoker透视... 5分钟新号!wepoker透视器免费(透视底牌)详细辅助外挂(确实是真的有挂)1、打开软件启动之后找...