代码随想录算法训练营第十天 | 232.用栈实现队列 225. 用队列实现栈 20. 有效的括号 1047. 删除字符串中的所有相邻重复项

今日任务:

 232.用栈实现队列 
 225. 用队列实现栈
 20. 有效的括号 
 1047. 删除字符串中的所有相邻重复项 

 232.用栈实现队列 

题目链接:. - 力扣(LeetCode)

class MyQueue {
    private Stack<Integer> stack;

    public MyQueue() {
        stack = new Stack<Integer>();
    }
    
    public void push(int x) {
        stack.push(x);
    }
    
    public int pop() {
        Stack<Integer> candidateStack = new Stack<Integer>();
        int res;
        while (!stack.isEmpty()) {
            candidateStack.push(stack.pop());
        }
        res = candidateStack.pop();
        while (!candidateStack.isEmpty()) {
            stack.push(candidateStack.pop());
        }
        return res;
    }
    
    public int peek() {
        Stack<Integer> candidateStack = new Stack<Integer>();
        int res;
        while (!stack.isEmpty()) {
            candidateStack.push(stack.pop());
        }
        res = candidateStack.peek();
        while (!candidateStack.isEmpty()) {
            stack.push(candidateStack.pop());
        }
        return res;
    }
    
    public boolean empty() {
        return stack.isEmpty();
    }

    // @Test
    // public void test() {
    //     LeetCode232 myQueue = new LeetCode232();
    //     myQueue.push(1);
    //     myQueue.push(2);
    //     System.out.println("The peek output is " + myQueue.peek());
    //     System.out.println("The pop output is " + myQueue.pop());
    //     System.out.println("The empty output is " + myQueue.empty());
    // }

}

/**
 * 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. 用队列实现栈

题目链接:. - 力扣(LeetCode)

class MyStack {
    private Queue<Integer> queue;
    public MyStack() {
        queue = new LinkedList<>();
    }
    
    public void push(int x) {
        queue.offer(x);
    }
    
    public int pop() {
        Queue<Integer> tempQueue = new LinkedList<>();
        int size = queue.size();
        for (int i = 0; i < size - 1; i++) {
            tempQueue.offer(queue.poll());
        }
        int res = queue.poll();
        while (!tempQueue.isEmpty()) {
            queue.offer(tempQueue.poll());
        }
        return res;
    }
    
    public int top() {
        Queue<Integer> tempQueue = new LinkedList<>();
        int size = queue.size();
        for (int i = 0; i < size - 1; i++) {
            tempQueue.offer(queue.poll());
        }
        int res = queue.peek();
        tempQueue.offer(queue.poll());
        while (!tempQueue.isEmpty()) {
            queue.offer(tempQueue.poll());
        }
        return res;
    }
    
    public boolean empty() {
        return queue.isEmpty();
    }

    // @Test
    // public void test() {
    //     LeetCode225 stack = new LeetCode225();
    //     stack.push(1);
    //     stack.push(2);
    //     System.out.println("The top output is " + stack.top());
    //     System.out.println("The pop output is " + stack.pop());
    //     System.out.println("The empty output is " + stack.empty());
    // }
}

/**
 * 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. 有效的括号 

题目链接:. - 力扣(LeetCode)

class Solution {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack();
        String leftStr = "({[";
        String rightStr = ")}]";

        for (int i = 0; i < s.length(); i++) {
            if (leftStr.indexOf(s.charAt(i)) != -1){
                stack.push(s.charAt(i));
            } else if (!stack.isEmpty()) {
                if (rightStr.indexOf(s.charAt(i)) != leftStr.indexOf(stack.pop())) {
                    return false;
                }
            } else {
                return false;
            }
        }
        return stack.isEmpty() == true ? true : false;
    }
}


 1047. 删除字符串中的所有相邻重复项 

题目链接:. - 力扣(LeetCode)

class Solution {
    public String removeDuplicates(String s) {
        Stack<Character> stack = new Stack<Character>();
        for (int i = 0; i < s.length(); i++) {
            if(!stack.isEmpty()){
                char ch = stack.peek();
                if (ch == s.charAt(i)) {
                    stack.pop();
                } else {
                    stack.push(s.charAt(i));
                }
            } else {
                stack.push(s.charAt(i));
            }
        }

        StringBuilder res = new StringBuilder();
        while (!stack.isEmpty()) {
            res.append(stack.pop());
        }
        return res.reverse().toString();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值