【数据结构】栈和队列习题

栈和队列习题

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;
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值