代码随想录算法训练营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();
    }
}
相关推荐
luthane1 小时前
python 实现word frequency functions词频函数算法
python·算法·word
youyiketing1 小时前
排序 算法
数据结构·算法·排序算法
东方冷哥1 小时前
考研数据结构——C语言实现冒泡排序
c语言·数据结构·算法
菜鸟求带飞_5 小时前
算法打卡:第十一章 图论part02
java·数据结构·算法·图论
luthane6 小时前
python 实现area under curve曲线下面积算法
开发语言·python·算法
循环渐进Forward6 小时前
【C++笔试强训】如何成为算法糕手Day2
开发语言·数据结构·c++·算法·哈希算法·笔试·牛客
Tisfy6 小时前
LeetCode 2207.字符串中最多数目的子序列:计数
算法·leetcode·字符串·题解·思维·计数
还有糕手8 小时前
算法【Dijkstra算法及分层图最短路】
数据结构·算法·图论·宽度优先
杰克尼8 小时前
371. 两整数之和
数据结构·算法