代码随想录算法训练营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();
    }
}
相关推荐
再卷也是菜8 分钟前
C++篇(14)二叉树进阶算法题
c++·算法
小邓儿◑.◑12 分钟前
贪心算法 | 每周8题(三)
算法·贪心算法
2401_8414956415 分钟前
【数据结构】最长的最短路径的求解
java·数据结构·c++·python·算法·最短路径·图搜索
小龙报18 分钟前
《算法每日一题(1)--- 连续因子》
c语言·开发语言·c++·windows·git·算法·visual studio
夜晚中的人海44 分钟前
【C++】滑动窗口算法习题
开发语言·c++·算法
violet-lz1 小时前
数据结构四大简单排序算法详解:直接插入排序、选择排序、基数排序和冒泡排序
数据结构·算法·排序算法
·白小白1 小时前
力扣(LeetCode) ——118.杨辉三角(C++)
c++·算法·leetcode
CoovallyAIHub2 小时前
超越“识别”:下一代机器视觉如何破解具身智能落地难题?
深度学习·算法·计算机视觉
仰泳的熊猫2 小时前
LeetCode:207. 课程表
数据结构·c++·算法·leetcode
liu****2 小时前
19.map和set的封装
开发语言·数据结构·c++·算法