/**
* Description:给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。
* 有效字符串需满足:左括号必须用相同类型的右括号闭合。左括号必须以正确的顺序闭合。
* 注意空字符串可被认为是有效字符串.
* 注意:一般与括号有关的问题,就考虑栈;一定要画图演示.
* 如果在main函数调用方法,此方法需要用static修饰;
*/
public class Solution {
public boolean isValid(String s) { //面试重点
//创建存储字符的栈
Stack<Character> stack = new Stack<>();
for (int i = 0; i < s.length(); i++) {
char charAt = s.charAt(i);
//如果是左括号就入栈
if (charAt == '(' || charAt == '[' || charAt == '{') {
stack.push(charAt);
continue;
} else {
if (stack.isEmpty()) {
return false;
} else {
//取栈顶元素,不删除
char top = stack.peek();
if (top == '(' && charAt == ')' || top == '[' && charAt == ']'
|| top == '{' && charAt == '}') {
stack.pop();
} else {
return false;
}
}
}
}
if (!stack.isEmpty()) {
//左括号多
return false;
}
return true;
}
public boolean isValid1(String s) {
//创建存储字符的栈
Stack<Character> stack = new Stack<>();
for (int i = 0; i < s.length(); i++) {
char charAt = s.charAt(i);
//如果是左括号就入栈
if (charAt == '(' || charAt == '[' || charAt == '{') {
stack.push(charAt);
continue;
}
//如果是右括号, 取出栈顶元素,取之前先判断栈是否为空
if (stack.isEmpty()) {
return false;
}
// 取栈顶元素,不删除
char top = stack.pop();
// 判断栈顶元素和当前元素是不是配对的
if (top == '(' && charAt == ')') {
continue;
}
if (top == '[' && charAt == ']') {
continue;
}
if (top == '{' && charAt == '}') {
continue;
}
return false;
}
if (stack.isEmpty()) {
return true;
}
return false;
}
}
/**
* Description:使用队列实现栈的下列操作:
* push(x) -- 元素 x 入栈
* pop() -- 移除栈顶元素
* top() -- 获取栈顶元素
* empty() -- 返回栈是否为空
*/
public class MyStack {
private Queue<Integer> queue1 = new LinkedList<>();
private Queue<Integer> queue2 = new LinkedList<>();
// Push element x onto stack.
//如果是第一次入栈,指定入到queue1,如果不是第一次,每次入到不为空的队列里面。
public void push(int x) {
if (!queue1.isEmpty()) {
queue1.offer(x);
} else if (!queue2.isEmpty()) {
queue2.offer(x);
} else {
queue1.offer(x);
}
}
/**
* Removes the element on top of the stack and returns that element.
*/
//把不为空的队列里面的元素倒入空的队列,最后一个元素不倒入;
//如果两个队列都为空的时候,不能出栈了。
public int pop() {
if (empty()) {
return -1;
}
int oldData = 0;
if (!queue1.isEmpty()) {
//为了保障size不变
int size = queue1.size();
for (int i = 0; i < size - 1; i++) { //size - 1--出的元素为size-1
/*int a = queue1.poll();
queue2.offer(a);*/
queue2.offer(queue1.poll());
}
oldData = queue1.poll();
} else if (!queue2.isEmpty()) {
int size = queue2.size();
for (int i = 0; i < size - 1; i++) {
queue1.offer(queue2.poll());
}
oldData = queue2.poll();
}
return oldData;
}
/**
* Get the top element.
*/
public int top() {
if (empty()) {
return -1;
}
int oldData = 0;
if (!queue1.isEmpty()) {
int size = queue1.size();
for (int i = 0; i < size; i++) {
//保存每次出栈的元素
oldData = queue1.poll();
queue2.offer(oldData);
}
} else if (!queue2.isEmpty()) {
int size = queue2.size();
for (int i = 0; i < size; i++) {
oldData = queue2.poll();
queue1.offer(oldData);
}
}
return oldData;
}
/**
* Returns whether the stack is empty.
*/
public boolean empty() {
if (queue1.isEmpty() && queue2.isEmpty()) {
return true;
}
return false;
}
}
/**
* Description:使用栈实现队列的下列操作:
* push(x) -- 将一个元素放入队列的尾部。
* pop() -- 从队列首部移除元素。
* peek() -- 返回队列首部的元素。
* empty() -- 返回队列是否为空。
*/
public class MyQueue {
public Stack<Integer> stack;
public Stack<Integer> stackTmp;
/**
* Initialize your data structure here.
*/
public MyQueue() {
stack = new Stack<>();
stackTmp = new Stack<>();
}
/**
* Push element x to the back of queue.
*/
public void push(int x) {
stack.push(x);
}
/**
* Removes the element from in front of queue and returns that element.
*/
public int pop() {
if (stackTmp.empty()) {
//将stack 栈当中的所有元素全部倒入 stackTmp;
while (!stack.empty()) {
stackTmp.push(stack.pop());
}
}
if (!stackTmp.empty()) {
//将stackTmp 的栈顶元素弹出-》出栈stackTmp.pop
return stackTmp.pop();
}
return -1;
}
/**
* Get the front element.
*/
public int peek() {
if (stackTmp.empty()) {
//将stack 栈当中的所有元素全部倒入 stackTmp;
while (!stack.empty()) {
stackTmp.push(stack.pop());
}
}
if (!stackTmp.empty()) {
//将stackTmp 的栈顶元素弹出-》出栈stackTmp.pop
return stackTmp.peek();
}
return -1;
}
/**
* Returns whether the queue is empty.
*/
public boolean empty() {
if (stack.empty() && stackTmp.empty()) {
return true;
}
return false;
}
}
/**
* Description:最小栈问题
*/
public class MinStack {
// stack 用来表示正常的栈中的数据.
// minStack 用来存储每一层 A 栈帧中对应的最小值
private Stack<Integer> stack = new Stack<>();
private Stack<Integer> minStack = new Stack<>();
public void push(int x) {
//1、stack栈内一定会入栈
stack.push(x);
//2、第一次minStack一定会放元素,其余的每次
if (minStack.isEmpty()) {
minStack.push(x);
return;
}
//往stack栈-》和minStack栈顶比较
// x <= minStack.peek()--->minStack.push(x);
int min = minStack.peek();
if (x < min) {
min = x;
}
minStack.push(min);
}
public void pop() {
if (stack.isEmpty()) {
return;
}
int num = stack.pop();
if (num == minStack.peek()) {
minStack.pop();
}
}
public int top() {
if (stack.isEmpty()) {
return -1;
}
return stack.peek();
}
public int getMin() {
if (minStack.isEmpty()) {
return -1;
}
return minStack.peek();
}
}
牛客--栈和队列练习题
最新推荐文章于 2021-10-13 20:49:21 发布