代码随想录Day10

lc232

package stackAndqueue;

import java.util.Stack;

public class lc232 {

    Stack<Integer> satackIn;
    Stack<Integer> stackOut;

    public  lc232(){
        satackIn=new Stack<>();
        stackOut=new Stack<>();
    }

    public void push(int x){
        satackIn.push(x);
    }



    public int pop(){
        dumpstackIn();
        return stackOut.pop();
    }

    public int peek(){
        dumpstackIn();
        return stackOut.peek();
    }
    public boolean empty(){
        return satackIn.isEmpty()&&stackOut.isEmpty();
    }

    private  void dumpstackIn(){
        if (!stackOut.isEmpty()){
            return;
        }
        while (!satackIn.isEmpty()){

            stackOut.push(satackIn.pop());

        }

    }
}

lc225

package stackAndqueue;

import java.util.LinkedList;
import java.util.Queue;

public class lc225 {
    Queue<Integer> queueIn;

    public lc225() {
        queueIn = new LinkedList<>();
    }
    public void push(int x){
        queueIn.add(x);
    }

    public int pop(){
        rePostion();
        return queueIn.poll();
    }

    public int top(){
        rePostion();
        int result = queueIn.poll();
        queueIn.add(result);
        return result;
    }

    public boolean empty(){
        return queueIn.isEmpty();
    }


    public void rePostion(){
        int size = queueIn.size();
        size--;
        while (size-->0){
            Integer poll = queueIn.poll();
            queueIn.add(poll);
        }
    }

}

lc20

package stackAndqueue;

import java.util.Stack;

public class lc20 {
    public boolean isval(String s){

        Stack<Character> stack=new Stack<>();
        if (s.length()%2!=0){
            return false;
        }
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c=='('){
                stack.push(')');
            }else if (c=='{'){
                stack.push('}');
            }else if (c=='['){
                stack.push(']');
            }else if (stack.isEmpty() || stack.peek()!=c){ //
                return false;
            }else {
                //遇到有括号而且相同
                stack.pop();
            }
        }

        return stack.isEmpty();
    }

}

lc1047

package stackAndqueue;

import java.util.Stack;

public class lc1047 {
    public String removeDuplicates(String S){
        Stack<Character> stack=new Stack<>();
        String str="";
        for (int i = 0; i < S.length(); i++) {
            char c = S.charAt(i);
            if (stack.isEmpty() || stack.peek()!=c){
                stack.push(c);
            }else {
                stack.pop();
            }
        }
        while (!stack.isEmpty()){
            str=stack.pop()+str;
        }
        return str;
    }
}

全部评论

相关推荐

头像
YoungZ英勇:Mysql,JDBC,spring,springmvc,springboot,mybatis,mybatisplus,springcloud,redis。 rabbitmq,rocketmq,kafka(三个会一个就行),JUC,JVM,再会点nginx,linux,docket等更好。 还有数据结构,算法,计算机网络,操作系统,设计模式,然后再做至少两个项目,背八股
点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务