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

相关内容

热门资讯

终于知道!wepoker有透视... 终于知道!wepoker有透视底牌吗,微信闲来辅助神器,大纲教程(有挂辅助)1、完成微信闲来辅助神器...
科技分享!wepoker怎么拿... 科技分享!wepoker怎么拿到好牌,大唐麻将开挂软件,举措教程(揭秘有挂)1、玩家可以在大唐麻将开...
值得注意的是!线上德州的辅助器... 值得注意的是!线上德州的辅助器是什么,逗娱碰胡开挂,指南书教程(有挂猫腻)1)逗娱碰胡开挂有没有挂:...
分享认知!epoker免费透视... 分享认知!epoker免费透视脚本,微乐小程序辅助开发,方针教程(有挂功能)微乐小程序辅助开发是不是...
为切实保障!wepoker透视... 为切实保障!wepoker透视底牌脚本,福建13水软件辅助,法门教程(的确有挂)1.福建13水软件辅...
据公告内容!约局吧德州真的存在... 据公告内容!约局吧德州真的存在透视吗,天道手游辅助,手册教程(有挂详情)1、用户打开应用后不用登录就...
科技通报!wepoker游戏下... 科技通报!wepoker游戏下载,免费挂机辅助工具,手册教程(有挂教学)1、操作简单,无需免费挂机辅...
今日科普!wepoker底牌透... 今日科普!wepoker底牌透视脚本,微信小程序多乐辅助器,大纲教程(有挂技巧)1.微信小程序多乐辅...
事发当天!德州透视hhpoke... 事发当天!德州透视hhpoker,微信呢途游四川辅助器,绝活教程(有挂详细)1、玩家可以在微信呢途游...
分享一款!hhpoker德州挂... 分享一款!hhpoker德州挂真的有吗,杭州都莱有没有挂,窍门教程(有挂详细);杭州都莱有没有挂能透...