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

232.用栈实现队列

请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(pushpoppeekempty):

实现 MyQueue 类:

  • void push(int x) 将元素 x 推到队列的末尾
  • int pop() 从队列的开头移除并返回元素
  • int peek() 返回队列开头的元素
  • boolean empty() 如果队列为空,返回 true ;否则,返回 false
class MyQueue {

    private Stack<Integer> inStack;//输入栈
    private Stack<Integer> outStack;//输出栈

    public MyQueue() {
        inStack = new Stack<>();
        outStack = new Stack<>();
    }
    
    public void push(int x) {//压入输入栈
        inStack.push(x);
    }
    
    public int pop() {//从输出栈弹出
        dumpToOutStack();
        return outStack.pop();
    }
    
    public int peek() {//读取输出栈顶元素
        dumpToOutStack();
        return outStack.peek();
    }
    
    public boolean empty() {//两栈都为空则队列为空
        return (inStack.empty() && outStack.empty());
    }
    public void dumpToOutStack() {//将输出栈中元素存入输出栈中
        if(!outStack.empty())
            return;
        while(!inStack.empty()) {
            outStack.push(inStack.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.用队列实现栈

请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(pushtoppopempty)。

实现 MyStack 类:

  • void push(int x) 将元素 x 压入栈顶。
  • int pop() 移除并返回栈顶元素。
  • int top() 返回栈顶元素。
  • boolean empty() 如果栈是空的,返回 true ;否则,返回 false
class MyStack {
    private Queue<Integer> queue;
    public MyStack() {
        queue = new LinkedList<>();
    }
    
    public void push(int x) {
        queue.add(x);
        int size = queue.size();
        while(size > 1) {//将新加入元素移动到队列头
            queue.add(queue.poll());
            size--;
        }
    }
    
    public int pop() {
        return queue.poll();
    }
    
    public int top() {
        return queue.peek();
    }
    
    public boolean empty() {
        return queue.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.有效的括号

给定一个只包括 '('')''{''}''['']' 的字符串 s ,判断字符串是否有效。

有效字符串需满足:

  1. 左括号必须用相同类型的右括号闭合。
  2. 左括号必须以正确的顺序闭合。
  3. 每个右括号都有一个对应的相同类型的左括号。
class Solution {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for(int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if(ch == '(')
                stack.push(')');
            else if(ch == '[')
                stack.push(']');
            else if(ch == '{')
                stack.push('}');
            else if(!stack.empty() && ch == stack.peek())
                //检查右括号是否与栈顶元素相等
                stack.pop();
            else 
                return false;               
        }
        return stack.empty();
    }
}

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

给出由小写字母组成的字符串 s重复项删除操作会选择两个相邻且相同的字母,并删除它们。

s 上反复执行重复项删除操作,直到无法继续删除。

在完成所有重复项删除操作后返回最终的字符串。答案保证唯一。

//使用栈
class Solution {
    public String removeDuplicates(String s) {
        StringBuilder result = new StringBuilder();//将结果作为栈
        int top = -1;//维护一个栈顶指针,初始值为-1
        for(int i = 0; i < s.length(); i++) {
            if(result.length() > 0 && result.charAt(top) == s.charAt(i)) {
                //与栈顶元素相等,弹出栈顶元素,检查下一元素
                result.deleteCharAt(top);
                top--;
                continue;
            }
            result.append(s.charAt(i));
            top++;
        }
        return result.toString();
    }
}
  • 可以使用双指针解决
class Solution {
    public String removeDuplicates(String s) {
        char[] chars = s.toCharArray();
        int fast = 0;//遍历元素
        int slow = 0;//修改元素
        while(fast < chars.length) {
            chars[slow] = chars[fast];
            if(slow > 0 && chars[slow] == chars[slow - 1]) {
                //相邻两元素相同,回退慢指针
                slow--;
            }else 
                slow++;
            fast++;
        }
        return new String(chars, 0, slow);
    }    
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值