代码随想录算法训练营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();
    }
}
相关推荐
hetao173383710 分钟前
ZYZ28-NOIP模拟赛-Round4 hetao1733837的record
c++·算法
Nebula_g10 分钟前
C语言应用实例:解方程(二分查找)
c语言·开发语言·学习·算法·二分查找·基础
少许极端1 小时前
算法奇妙屋(十)-队列+宽搜(BFS)
java·数据结构·算法·bfs·宽度优先·队列
想唱rap3 小时前
Linux开发工具(4)
linux·运维·服务器·开发语言·算法
前端炒粉3 小时前
21.搜索二维矩阵 II
前端·javascript·算法·矩阵
星释3 小时前
Rust 练习册 :Rail Fence Cipher与栅栏密码
开发语言·算法·rust
东方隐侠安全团队-千里3 小时前
第3节 RSA算法开启公钥加密时代
网络·人工智能·算法
7澄14 小时前
深入解析 LeetCode 1:两数之和
算法·leetcode·职场和发展·arraylist
Moonbit4 小时前
MGPIC 初赛提交倒计时 4 天!
后端·算法·编程语言