代码随想录算法训练营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();
    }
}
相关推荐
小O的算法实验室21 小时前
2026年ASOC,基于深度强化学习的无人机三维复杂环境分层自适应导航规划方法,深度解析+性能实测
算法·无人机·论文复现·智能算法·智能算法改进
郭涤生1 天前
STL vector 扩容机制与自定义内存分配器设计分析
c++·算法
༾冬瓜大侠༿1 天前
vector
c语言·开发语言·数据结构·c++·算法
Ricky111zzz1 天前
leetcode学python记录1
python·算法·leetcode·职场和发展
汀、人工智能1 天前
[特殊字符] 第58课:两个正序数组的中位数
数据结构·算法·数据库架构··数据流·两个正序数组的中位数
liu****1 天前
第16届省赛蓝桥杯大赛C/C++大学B组(京津冀)
开发语言·数据结构·c++·算法·蓝桥杯
汀、人工智能1 天前
[特殊字符] 第79课:分割等和子集
数据结构·算法·数据库架构·位运算·哈希表·分割等和子集
汀、人工智能1 天前
[特殊字符] 第74课:完全平方数
数据结构·算法·数据库架构·图论·bfs·完全平方数
CoderCodingNo1 天前
【GESP】C++四、五级练习题 luogu-P1177 【模板】排序
数据结构·c++·算法
Proxy_ZZ01 天前
从零实现LDPC比特翻转译码器:C语言实战与底层逻辑解析
c语言·算法