1. 括号匹配问题
给定一个只包括 ‘(’,’)’,’{’,’}’,’[’,’]’ 的字符串 s ,判断字符串是否有效。
有效字符串需满足:
- 左括号必须用相同类型的右括号闭合。
- 左括号必须以正确的顺序闭合。
示例 1:
输入:s = "()"
输出:true
示例 2:
输入:s = "()[]{}"
输出:true
示例 3:
输入:s = "(]"
输出:false
示例 4:
输入:s = "([)]"
输出:false
示例 5:
输入:s = "{[]}"
输出:true
链接: 括号匹配问题.
class Solution {
public boolean isValid(String s) {
if(s == null) return false;
Stack<Character> stack = new Stack<>();
for(int i = 0 ;i < s.length() ; i++ ) {
char ch = s.charAt(i);
if(ch == '(' || ch == '[' || ch == '{') {
stack.push(ch);
} else {
if(stack.empty()) {
System.out.println("右括号多");
return false;
} else{
//对比
char top = stack.peek();
if(top=='(' && ch==')'
|| top=='[' && ch==']'
|| top=='{' && ch=='}')
{
stack.pop();
}
else {
System.out.println("左右括号不匹配");
return false;
}
}
}
}
if(!stack.empty()) {
System.out.println("左括号多");
return false;
}
return true;
}
}
2. 用队列实现栈
请使用两个队列实现一个后入先出(LIFO)的栈,并支持普通队列的全部四种操作(push、top、pop 和 empty)。
实现 MyStack 类:
- void push(int x) 将元素 x 压入栈顶。
- int pop() 移除并返回栈顶元素。
- int top() 返回栈顶元素。
- boolean empty() 如果栈是空的,返回 true ;否则,返回 false 。
示例:
输入:
["MyStack", "push", "push", "top", "pop", "empty"]
[[], [1], [2], [], [], []]
输出:
[null, null, null, 2, 2, false]
解释:
MyStack myStack = new MyStack();
myStack.push(1);
myStack.push(2);
myStack.top(); // 返回 2
myStack.pop(); // 返回 2
myStack.empty(); // 返回 False
链接: 用队列实现栈.
class MyStack {
public Queue<Integer> qu1;
public Queue<Integer> qu2;
/** Initialize your data structure here. */
public MyStack() {
qu1 = new LinkedList<>();
qu2 = new LinkedList<>();
}
/** Push element x onto stack. */
public void push(int x) {
if(!qu1.isEmpty()) {
qu1.offer(x);
} else if(!qu2.isEmpty()) {
qu2.offer(x);
} else {
qu2.offer(x);
}
}
/** Removes the element on top of the stack and returns that element. */
public int pop() {
if(empty()) {
return -1;
}
if(!qu1.isEmpty()) {
int ret = 0;
int len = qu1.size();
for(int i = 0; i<len-1; i++ ) {
ret = qu1.poll();
qu2.offer(ret);
}
return qu1.poll();
} else{
int ret = 0;
int len = qu2.size();
for(int i = 0; i<len-1; i++ ) {
ret = qu2.poll();
qu1.offer(ret);
}
return qu2.poll();
}
}
/** Get the top element. */
public int top() {
if(empty()) {
return -1;
}
if(!qu1.isEmpty()) {
int ret = 0;
int len = qu1.size();
for(int i = 0; i<len; i++ ) {
ret = qu1.poll();
qu2.offer(ret);
}
return ret;
} else{
int ret = 0;
int len = qu2.size();
for(int i = 0; i<len; i++ ) {
ret = qu2.poll();
qu1.offer(ret);
}
return ret;
}
}
/** Returns whether the stack is empty. */
public boolean empty() {
return qu1.isEmpty() && qu2.isEmpty();
}
}
3. 用栈实现队列
请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty):
实现 MyQueue 类:
- void push(int x) 将元素 x 推到队列的末尾
- int pop() 从队列的开头移除并返回元素
- int peek() 返回队列开头的元素
- boolean empty() 如果队列为空,返回 true ;否则,返回 false
示例:
输入:
["MyQueue", "push", "push", "peek", "pop", "empty"]
[[], [1], [2], [], [], []]
输出:
[null, null, null, 1, 1, false]
解释:
MyQueue myQueue = new MyQueue();
myQueue.push(1); // queue is: [1]
myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
myQueue.peek(); // return 1
myQueue.pop(); // return 1, queue is [2]
myQueue.empty(); // return false
链接: 用栈实现队列.
class MyQueue {
public Stack<Integer> s1;
public Stack<Integer> s2;
/** Initialize your data structure here. */
public MyQueue() {
s1 = new Stack<>();
s2 = new Stack<>();
}
/** Push element x to the back of queue. */
public void push(int x) {
s1.push(x);
}
/** Removes the element from in front of queue and returns that element. */
public int pop() {
if(empty()) {
return -1;
}
if(s2.empty()) {
while(!s1.empty()) {
s2.push(s1.pop());
}
}
return s2.pop();
}
/** Get the front element. */
public int peek() {
if(empty()) {
return -1;
}
if(s2.empty()) {
while(!s1.empty()) {
s2.push(s1.pop());
}
}
return s2.peek();
}
/** Returns whether the queue is empty. */
public boolean empty() {
return s1.empty() && s2.empty();
}
}