代码随想录算法训练营Day9

232.用栈实现队列

Collection------List------Vector类------Stack类

java 复制代码
class MyQueue {
    Stack<Integer> stackIn;
    Stack<Integer> stackOut;
    public MyQueue() {
        stackIn=new Stack();
        stackOut=new Stack();
    } 
    public void push(int x) {
        stackIn.push(x);
    }
    
    public int pop() {
        notempty();
        return stackOut.pop();
    }
    
    public int peek() {
        notempty();
        return stackOut.peek();
    }
    
    public boolean empty() {
        if(stackIn.isEmpty()&&stackOut.isEmpty())
        return true;
        else
        return false;

    }
    public void notempty(){
        if(!stackOut.isEmpty())
        return;
        while(!stackIn.isEmpty()){
            stackOut.push(stackIn.pop());
        }
    }
}

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue obj = new MyQueue();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.peek();
 * boolean param_4 = obj.empty();
 */

225. 用队列实现栈

Collection------Queue------LinkedList类

java 复制代码
class MyStack {
    Queue<Integer> myStack;
    public MyStack() {
    myStack=new LinkedList();
    }
    public void push(int x) {
        myStack.offer(x);
        int size=myStack.size();
        for(int i=0;i<size-1;i++){
            myStack.offer(myStack.poll());
        }
    }  
    public int pop() {
        return myStack.poll();
    } 
    public int top() {
        return myStack.peek();
    }
    public boolean empty() {
        return myStack.isEmpty();
    }
}
/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack obj = new MyStack();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.top();
 * boolean param_4 = obj.empty();
 */

20. 有效的括号

Collection------Deque------LinkedList类

java 复制代码
class Solution {
    public boolean isValid(String s) {
        Deque<Character> mystack=new LinkedList();
        char a;
        char[] ch=s.toCharArray();
        for(int i=0;i<ch.length;i++){
            a=ch[i];
            if(a=='('){
                mystack.push(')');
            }
            else if(a=='{'){
                mystack.push('}');
            }
            else if(a=='['){
                mystack.push(']');
            }
            else if(mystack.isEmpty()||mystack.peek()!=a){
                return false;
            }
            else{
                   mystack.pop();  
            }
        }
        return mystack.isEmpty();
    }
}

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

StringBuilder(StringBuffer)

java 复制代码
class Solution {
    public String removeDuplicates(String s) {
        StringBuffer sb=new StringBuffer();
        int index=-1;
        for(int i=0;i<s.length();i++){         
            char ch=s.charAt(i);
            if(sb.length()==0||sb.charAt(index)!=ch){
                index++;
                sb.append(ch);
            }else{
                sb.deleteCharAt(index);
                index--;
            }
        }
        return sb.toString();
    }
}
相关推荐
夏鹏今天学习了吗7 小时前
【LeetCode热题100(82/100)】单词拆分
算法·leetcode·职场和发展
mit6.8248 小时前
mysql exe
算法
2501_901147838 小时前
动态规划在整除子集问题中的应用与高性能实现分析
算法·职场和发展·动态规划
中草药z9 小时前
【嵌入模型】概念、应用与两大 AI 开源社区(Hugging Face / 魔塔)
人工智能·算法·机器学习·数据集·向量·嵌入模型
知乎的哥廷根数学学派9 小时前
基于数据驱动的自适应正交小波基优化算法(Python)
开发语言·网络·人工智能·pytorch·python·深度学习·算法
ADI_OP9 小时前
ADAU1452的开发教程10:逻辑算法模块
算法·adi dsp中文资料·adi dsp·adi音频dsp·adi dsp开发教程·sigmadsp的开发详解
xingzhemengyou110 小时前
C语言 查找一个字符在字符串中第i次出现的位置
c语言·算法
小六子成长记11 小时前
【C++】:搜索二叉树的模拟实现
数据结构·c++·算法
汉克老师12 小时前
GESP2025年9月认证C++二级真题与解析(编程题1(优美的数字))
c++·算法·整除·枚举算法·求余·拆数
Zevalin爱灰灰12 小时前
现代控制理论——第二章 系统状态空间表达式的解
线性代数·算法·现代控制