- 面试题
1.题目描述
给定一个只包括 ‘(’,’)’,’{’,’}’,’[’,’]’ 的字符串,判断字符串是否有效。
有效字符串需满足:
左括号必须用相同类型的右括号闭合。
左括号必须以正确的顺序闭合。
注意空字符串可被认为是有效字符串。
题目分析
程序代码
import java.util.Deque;
import java.util.LinkedList;
class Solution {
public boolean compareBracket(char left,char right) {
if(left == '(' &&right == ')') {
return true;
}
if(left == '[' &&right == ']') {
return true;
}
if(left == '{' &&right == '}') {
return true;
}
return false;
}
public boolean isValid(String s) {
//1.准备好一个栈
Deque<Character> stack = new LinkedList<>();
//遍历1
// for (int i = 0; i < s.length(); i++) {
// char c = s.charAt(i);
// }
//遍历2
char[] charArray = s.toCharArray();
for(char c : charArray) {
switch (c) {
case '(':
case '[':
case '{':
stack.push(c);
break;
default:{
if(stack.isEmpty()) {
return false;
}
char left = stack.pop();
if(!compareBracket(left,c)) {
return false;
}
}
}
}
if(stack.isEmpty()) {
return true;
} else {
return false;
}
}
}
2.用队列实现栈
使用队列实现栈的下列操作:
push(x) -- 元素 x 入栈
pop() -- 移除栈顶元素
top() -- 获取栈顶元素
empty() -- 返回栈是否为空
程序代码
import java.util.LinkedList;
import java.util.Queue;
class MyStack {
private Queue<Integer> queue;
/** Initialize your data structure here. */
public MyStack() {
queue = new LinkedList<>();
}
/** Push element x onto stack. */
public void push(int x) {
queue.add(x);
}
/** Removes the element on top of the stack and returns that element. */
public int pop() {
int size = queue.size();
for(int i = 0;i < size - 1;i++) {
Integer e = queue.remove();
queue.add(e);
}
return queue.remove();
}
/** Get the top element. */
public int top() {
int size = queue.size();
for(int i = 0;i < size - 1;i++) {
Integer e = queue.remove();
queue.add(e);
}
Integer t = queue.remove();
queue.add(t);
return t;
}
/** Returns whether the stack is empty. */
public boolean empty() {
return queue.isEmpty();
}
}
3.用栈实现队列
使用栈实现队列的下列操作:
push(x) -- 将一个元素放入队列的尾部。
pop() -- 从队列首部移除元素。
peek() -- 返回队列首部的元素。
empty() -- 返回队列是否为空。
程序代码
class MyQueue {
private Deque<Integer> s1;//准备出的
private Deque<Integer> s2;//优先放的
/** Initialize your data structure here. */
public MyQueue() {
s1 = new LinkedList<>();
s2 = new LinkedList<>();
}
/** Push element x to the back of queue. */
public void push(int x) {
s2.push(x);
}
/** Removes the element from in front of queue and returns that element. */
public int pop() {
if(s1.isEmpty()) {
while (!s2.isEmpty()) {
Integer e = s2.pop();
s1.push(e);
}
}
return s1.pop();
}
/** Get the front element. */
public int peek() {
if(s1.isEmpty()) {
while (!s2.isEmpty()) {
Integer e = s2.pop();
s1.push(e);
}
}
return s1.peek();
}
/** Returns whether the queue is empty. */
public boolean empty() {
return s1.isEmpty() && s2.isEmpty();
}
}
4.实现一个最小栈
设计一个支持 push ,pop ,top 操作,并能在常数时间内检索到最小元素的栈。
push(x) —— 将元素 x 推入栈中。
pop() —— 删除栈顶的元素。
top() —— 获取栈顶元素。
getMin() —— 检索栈中的最小元素。
程序代码
class MinStack {
Deque<Integer> xStack;
Deque<Integer> minStack;
public MinStack() {
xStack = new LinkedList<Integer>();
minStack = new LinkedList<Integer>();
minStack.push(Integer.MAX_VALUE);
}
public void push(int x) {
xStack.push(x);
minStack.push(Math.min(minStack.peek(), x));
}
public void pop() {
xStack.pop();
minStack.pop();
}
public int top() {
return xStack.peek();
}
public int getMin() {
return minStack.peek();
}
}