Bootstrap

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

232.用栈实现队列

leetcode:  232. 用栈实现队列 - 力扣(LeetCode)

思路:

使用两个栈stack1和stack2

1. push(x): 就直接吧元素x放入stack1中

2. pop(): 要从队列首部移除元素,就把stack1中的元素,挨个弹出压入stack2,然后从stack2中弹出一个元素。(要先判断stack1是否为空,不为空继续弹)

如果stack2不为空,直接pop() stack2中的元素

3. peek(): 和pop()差不多,只不过不弹元素只返回

4. empty(): 判断两个stack都是否为空

class MyQueue {
    // 232. 用栈实现队列
    Stack<Integer> stack1;
    Stack<Integer> stack2;
    public MyQueue() {
        stack1 = new Stack<Integer>();
        stack2 = new Stack<Integer>();
    }
    
    public void push(int x) {
        stack1.push(x);
    }
    
    public int pop() {
        //如果stack2不为空,直接返回栈顶元素
    	if(!stack2.isEmpty()) return stack2.pop();
        //如果stack2为空,则把stack1中的元素加到stack2中
    	while(!stack1.isEmpty()) {
    		stack2.push(stack1.pop());
    	}
		return stack2.pop();
    }
    
    public int peek() {
        //如果stack2不为空,直接返回栈顶元素
    	if(!stack2.isEmpty()) return stack2.peek();
        //如果stack2为空,则把stack1中的元素加到stack2中
    	while(!stack1.isEmpty()) {
    		stack2.push(stack1.pop());
    	}
    	return stack2.peek();
    }
    
    public boolean empty() {
    	if(stack1.isEmpty() && stack2.isEmpty())
    		return true;
		return false;
    }
}

225. 用队列实现栈

leetcode: 225. 用队列实现栈 - 力扣(LeetCode)

思路:

使用两个队列queue1(放和push顺序相反的元素列表)和queue2(辅助队列)

1. push(x): 先把元素x放入queue2,然后判断1是否为空,为空的话,就把2赋给1,如果不为空,那么就把1的元素poll到2中,再把2赋给1。如此操作可以确保1中放和push顺序相反的元素列表。

2. pop(): 1直接poll

3. top(): 1直接peak

4. emtpy(): 判断queue1是否为空

import java.util.ArrayDeque;
import java.util.Queue;

class MyStack {
	//225. 用队列实现栈
    Queue<Integer> queue1;
    Queue<Integer> queue2;
    public MyStack() {
    	queue1 = new ArrayDeque<Integer>();
    	queue2 = new ArrayDeque<Integer>();
    }
    
    public void push(int x) {
        queue2.offer(x);
        while (!queue1.isEmpty()){
            queue2.offer(queue1.poll());
        }
        Queue<Integer> temp;
        temp = queue1;
        queue1 = queue2;
        queue2 = temp;
    }
    
    public int pop() {
		return queue1.poll();
    }
    
    public int top() {
		return queue1.peek();
    }
    
    public boolean empty() {
		return queue1.isEmpty();
        
    }
}

20. 有效的括号

20. 有效的括号 - 力扣(LeetCode)

思路:

1. for循环,如果是{[(就压入栈中

2. 如果遇到右括号,就pop栈顶元素并判断是否匹配

3. 最后,栈为空,则为有效字符串,否则,无效

    //20.有效的括号
    public boolean isValid(String s) {
    	Stack<Character> stack = new Stack<Character>();
    	
    	for (char c : s.toCharArray()) {
    		// 1. for循环,如果是{[(就压入栈中
    		if (c == '(' || c == '[' || c == '{') {
    			stack.add(c);
    		} else {
    			if (stack.isEmpty()) {
    				return false;
    			}
    			// 2. 如果遇到右括号,就pop栈顶元素并判断是否匹配
    			char top = stack.pop();
    			if (c == ')' && top != '(') 
    				return false;
    			if (c == ']' && top != '[') 
    				return false;
    			if (c == '}' && top != '{')
    				return false;
    		}
    	}
    	// 3. 最后,栈为空,则为有效字符串,否则,无效
		return stack.isEmpty();
    }

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

leetcode: 1047. 删除字符串中的所有相邻重复项 - 力扣(LeetCode)

思路:使用栈,循环遍历字符串,如果当前字符和栈顶元素一样,则pop栈顶元素,如果不一样,就push进去。

    // 1047. 删除字符串中的所有相邻重复项
    public String removeDuplicates(String s) {
    	Stack<Character> stack = new Stack<Character>();
    	for(char c : s.toCharArray()) {
    		if (stack.isEmpty()) {
    			stack.add(c);
    			continue;
    		}
    		if (c != stack.peek()) {
    			stack.push(c);
    		} else {
    			stack.pop();
    		}
    	}
    	StringBuilder sb = new StringBuilder();
    	while (!stack.isEmpty()) {
    		sb.append(stack.pop());
    	}
    	sb = sb.reverse();
    	return sb.toString();
    }
;