栈和队列习题
1.括号匹配的问题
题目链接
代码思路:
遍历字符串的每个字符
如果是左括号: 入栈
如果是右括号:1.如果栈是空的: 不匹配(右括号多了) 2.出栈
如果左右括号不匹配:不匹配
否则继续循环
如果栈不为空:不匹配(左括号多)否则匹配
代码实现
public class Solution
{
public boolean isValid(String s)
{
ArrayList<Character> stack = new ArrayList<>();
for (int i = 0; i < s.length(); i++)
{
char c = s.charAt(i);
switch (c)
{
case '(':
case '[':
case '{':
stack.add(c);
break;
case ')':
case ']':
case '}':
{
if (stack.isEmpty())
{
return false;
}
char left = stack.remove(stack.size() - 1);
if (!((left == '(' && c == ')')|| (left == '[' && c == ']') || (left == '{' && c == '}')))
{
return false;
}
break;
}
default:
break;
}
}
if (stack.isEmpty())
{
return true;
}
else
{
return false;
}
}
}
2.用队列实现栈
题目链接
class MyStack
{
private LinkedList<Integer> queue;
/** Initialize your data structure here. */
public MyStack()
{
queue = new LinkedList<>();
}
/** Push element x onto stack. */
public void push(int x)
{
queue.addLast(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++)
{
int v = queue.pollFirst();
queue.addLast(v);
}
return queue.pollFirst();
}
/** Get the top element. */
public int top()
{
int size = queue.size();
for (int i = 0; i < size - 1; i++)
{
int v = queue.pollFirst();
queue.addLast(v);
}
int v = queue.pollFirst();
queue.addLast(v);
return v;
}
/** Returns whether the stack is empty. */
public boolean empty()
{
return queue.isEmpty();
}
}
3.用栈实现队列
题目链接
public class MyQueue
{
private ArrayList<Integer> in;
private ArrayList<Integer> out;
public MyQueue()
{
in = new ArrayList<Integer>();
out = new ArrayList<Integer>();
}
public void push(int x)
{
in.add(x);
}
public int pop()
{
if (out.isEmpty())
{
int size = in.size();
for (int i = 0; i < size; i++)
{
int v = in.remove(in.size() - 1);
out.add(v);
}
}
return out.remove(out.size() - 1);
}
public int peek()
{
if (out.isEmpty())
{
int size = in.size();
for (int i = 0; i < size; i++)
{
int v = in.remove(in.size() - 1);
out.add(v);
}
}
return out.get(out.size() - 1);
}
public boolean empty()
{
return in.isEmpty() && out.isEmpty();
}
}
4.实现一个最小栈
题目链接
class MinStack
{
private ArrayList<Integer> normal;
private ArrayList<Integer> min;
public MinStack()
{
normal = new ArrayList<>();
min = new ArrayList<>();
}
public void push(int x)
{
normal.add(x);
if (min.isEmpty())
{
min.add(x);
} else if (x < min.get(min.size() - 1))
{
min.add(x);
}
else
{
min.add(min.get(min.size() - 1));
}
}
public void pop()
{
min.remove(min.size() - 1);
normal.remove(normal.size() - 1);
}
public int top()
{
return normal.get(normal.size() - 1);
}
public int getMin()
{
return min.get(min.size() - 1);
}
}
5.设计循环队列
题目链接
//基于数组实现
class MyCircularQueue
{
private int[] array;
private int size;
private int front;
private int rear;
/** Initialize your data structure here. Set the size of the queue to be k. */
public MyCircularQueue(int k)
{
array = new int[k];
size = 0;
front = 0;
rear = 0;
}
/** Insert an element into the circular queue. Return true if the operation is successful. */
public boolean enQueue(int value)
{
if (size == array.length)
{
return false;
}
array[rear] = value;
rear = (rear + 1) % array.length;
size++;
return true;
}
/** Delete an element from the circular queue. Return true if the operation is successful. */
public boolean deQueue()
{
if (size == 0)
{
return false;
}
front = (front + 1) % array.length;
size--;
return true;
}
/** Get the front item from the queue. */
public int Front()
{
if (size == 0)
{
return -1;
}
return array[front];
}
/** Get the last item from the queue. */
public int Rear()
{
if (size == 0)
{
return -1;
}
int index = (rear + array.length - 1) % array.length;
return array[index];
}
/** Checks whether the circular queue is empty or not. */
public boolean isEmpty()
{
return size == 0;
}
/** Checks whether the circular queue is full or not. */
public boolean isFull()
{
return size == array.length;
}
}