【LeetCode每日一题】——剑指Offer09.用两个栈实现队列

一【题目类别】

二【题目难度】

  • 简单

三【题目编号】

  • 剑指Offer09.用两个栈实现队列

四【题目描述】

  • 用两个栈实现一个队列。队列的声明如下,请实现它的两个函数 appendTail 和 deleteHead ,分别完成在队列尾部插入整数和在队列头部删除整数的功能。(若队列中没有元素,deleteHead 操作返回 -1 )

五【题目示例】

  • 示例 1:
    输入:
    [“CQueue”,“appendTail”,“deleteHead”,“deleteHead”]
    [[],[3],[],[]]
    输出:[null,null,3,-1]
  • 示例 2:
    输入:
    [“CQueue”,“deleteHead”,“appendTail”,“appendTail”,“deleteHead”,“deleteHead”]
    [[],[],[5],[2],[],[]]
    输出:[null,-1,null,null,5,2]

六【题目提示】

  • 1 <= values <= 10000
  • 最多会对 appendTail、deleteHead 进行 10000 次调用

七【解题思路】

  • 借助一个辅助栈,主栈正常入栈元素,但是每次出栈的时候将主栈的所有元素都push到辅助栈,变为先进先出的结构,并且要保证辅助栈空的时候才能加入,因为可以看作是一段一段来的,第一段的还没有出辅助栈栈,后面的不能入辅助栈,否则破坏了先进先出的结构

八【时间频度】

  • 时间复杂度: O ( 1 ) O(1) O(1)

九【代码实现】

  1. Java语言版
package Stack;

import java.util.Stack;

public class o09_UsingTwoStacksToImplementQueues {

    public static void main(String[] args) {
        CQueue cQueue = new CQueue();
        int res1 = cQueue.deleteHead();
        System.out.println(res1);
        cQueue.appendTail(5);
        cQueue.appendTail(2);
        int res2 = cQueue.deleteHead();
        System.out.println(res2);
        int res3 = cQueue.deleteHead();
        System.out.println(res3);
    }

}

class CQueue {

    // 新建一个进栈的栈和出栈的栈
    Stack<Integer> stack_in;
    Stack<Integer> stack_out;

    public CQueue() {
        stack_in = new Stack<Integer>();
        stack_out = new Stack<Integer>();
    }

    /**
     * 向stack_in添加元素,先进后出
     *
     * @param value 要添加的元素
     */
    public void appendTail(int value) {
        stack_in.add(value);
    }

    /**
     * stack_out出栈,因为队列是先进先出,所以用两个栈模拟数组
     * 本质就是stack_in进入的最后一个元素(也就是先进的元素)
     * 作为stack_out的第一个元素(也就是先出的元素)
     * 也就实现了,元素的先进先出,从而模拟队列
     *
     * @return
     */
    public int deleteHead() {
        if (stack_out.isEmpty()) {
            if (stack_in.isEmpty()) {
                return -1;
            }
            while (!stack_in.isEmpty()) {
                stack_out.push(stack_in.pop());
            }
            return stack_out.pop();
        } else {
            return stack_out.pop();
        }
    }

}
  1. C语言版
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

/*两个栈的结构体*/
typedef struct
{
	int stackIn_count;
	int stackOut_count;
	int *stackIn;
	int *stackOut;
} CQueue;

/*创建两个栈*/
CQueue* cQueueCreate()
{
	CQueue *obj = (CQueue *)malloc(sizeof(CQueue));
	obj->stackIn_count = 0;
	obj->stackOut_count = 0;
	obj->stackIn = (int *)malloc(sizeof(int) * 10000);
	obj->stackOut = (int *)malloc(sizeof(int) * 10000);
	return obj;
}

/*在stackIn的尾部添加数据*/
void cQueueAppendTail(CQueue* obj, int value)
{
	obj->stackIn[obj->stackIn_count++] = value;
}

/*在stackOut的头部弹出元素*/
int cQueueDeleteHead(CQueue* obj)
{
	if (obj->stackOut_count == 0)
	{
		if (obj->stackIn_count == 0)
		{
			return -1;
		}
		while (obj->stackIn_count != 0)
		{
			obj->stackOut[obj->stackOut_count++] = obj->stackIn[--obj->stackIn_count];
		}
	}
	return obj->stackOut[--obj->stackOut_count];
}

/*释放空间*/
void cQueueFree(CQueue* obj)
{
	free(obj->stackIn);
	free(obj->stackOut);
	free(obj);
}

/*主函数省略*/

十【提交结果】

  1. Java语言版
    在这里插入图片描述

  2. C语言版
    在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IronmanJay

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值