首页 > [剑指offer小分队]坚持第四天
头像
我是程序员小贱
发布于 2020-06-29 19:06
+ 关注

[剑指offer小分队]坚持第四天

<section data&#45;tool="mdnice编辑器" data&#45;website="https&#58;&#47;&#47;www&#46;mdnice&#46;com" id="nice" style="font&#45;size&#58; 16px&#59; color&#58; black&#59; padding&#58; 0 10px&#59; line&#45;height&#58; 1&#46;6&#59; word&#45;spacing&#58; 0px&#59; letter&#45;spacing&#58; 0px&#59; word&#45;break&#58; break&#45;word&#59; word&#45;wrap&#58; break&#45;word&#59; text&#45;align&#58; left&#59; font&#45;family&#58; Optima&#45;Regular&#44; Optima&#44; PingFangSC&#45;light&#44; PingFangTC&#45;light&#44; &#39;PingFang SC&#39;&#44; Cambria&#44; Cochin&#44; Georgia&#44; Times&#44; &#39;Times New Roman&#39;&#44; serif&#59; margin&#45;top&#58; &#45;10px&#59;">

今天开始讲和大家坚持打卡面试非常重要算法练习---剑指offer,希望我们能一起肝。

  • <section style="margin&#45;top&#58; 5px&#59; margin&#45;bottom&#58; 5px&#59; line&#45;height&#58; 26px&#59; text&#45;align&#58; left&#59; color&#58; rgb&#40;1&#44;1&#44;1&#41;&#59; font&#45;weight&#58; 500&#59;">

    栈 栈是一种先进后出的数据结构。栈中的元素从栈顶(top)压入(push),从栈顶弹出(top)。注意:压入我们需要判栈是否已满,弹出需要判栈是否已空。下面我们看看图加深下印象。 在这里插入图片描述 在这里插入图片描述 入栈 1 2 3 出栈 3 2 1

    </section>
  • <section style="margin&#45;top&#58; 5px&#59; margin&#45;bottom&#58; 5px&#59; line&#45;height&#58; 26px&#59; text&#45;align&#58; left&#59; color&#58; rgb&#40;1&#44;1&#44;1&#41;&#59; font&#45;weight&#58; 500&#59;">

    队列 队列是一种先进先出的数据结构。队列中的元素从队尾(rear)入队(push),从队头(front)出队(pop).同样,我们用图的方式来加深印象。 在这里插入图片描述 在这里插入图片描述 入队列 1 2 3 出队列 1 2 3

    </section>

一 题目

使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部。 pop() -- 从队列首部移除元素。 peek() -- 返回队列首部的元素。 empty() -- 返回队列是否为空。

示例

示例: MyQueue queue = new MyQueue(); queue.push(1); queue.push(2);
queue.peek(); // 返回 1 queue.pop(); // 返回 1 queue.empty(); // 返回 false

2 题目解析

  • <section style="margin&#45;top&#58; 5px&#59; margin&#45;bottom&#58; 5px&#59; line&#45;height&#58; 26px&#59; text&#45;align&#58; left&#59; color&#58; rgb&#40;1&#44;1&#44;1&#41;&#59; font&#45;weight&#58; 500&#59;">相信大家已经知道了栈是先进后出,队列是先进先出。假设1 2 3进栈,第一次出栈为3 2 1(反了一次),但是我们想要的出来的顺序为1 2 3,那么我们在此基础再反一次。 在这里插入图片描述</section>
  • <section style="margin&#45;top&#58; 5px&#59; margin&#45;bottom&#58; 5px&#59; line&#45;height&#58; 26px&#59; text&#45;align&#58; left&#59; color&#58; rgb&#40;1&#44;1&#44;1&#41;&#59; font&#45;weight&#58; 500&#59;">ok,其实我们需要引入另一栈来辅助我们完成,如下图。 在这里插入图片描述</section>

3 动画演示

不好意思哈,因为支持视频只支持b站,腾讯视频,可是我还没上传,我就放我公众号的外链吧。 动画演示

3 代码实现

  • <section style="margin&#45;top&#58; 5px&#59; margin&#45;bottom&#58; 5px&#59; line&#45;height&#58; 26px&#59; text&#45;align&#58; left&#59; color&#58; rgb&#40;1&#44;1&#44;1&#41;&#59; font&#45;weight&#58; 500&#59;">c++版本 在这里插入图片描述</section>
  • <section style="margin&#45;top&#58; 5px&#59; margin&#45;bottom&#58; 5px&#59; line&#45;height&#58; 26px&#59; text&#45;align&#58; left&#59; color&#58; rgb&#40;1&#44;1&#44;1&#41;&#59; font&#45;weight&#58; 500&#59;">python版本 在这里插入图片描述</section>
  • <section style="margin&#45;top&#58; 5px&#59; margin&#45;bottom&#58; 5px&#59; line&#45;height&#58; 26px&#59; text&#45;align&#58; left&#59; color&#58; rgb&#40;1&#44;1&#44;1&#41;&#59; font&#45;weight&#58; 500&#59;">java版本 在这里插入图片描述</section>

5 唠嗑

2020年6月29日打卡,打卡格式"打卡XX天"。暖蓝汇聚大家一起,探讨简历修改,面试经历分享,尽全力让大家能在2020找到理想的工作。如果你想加入,加我拉你进面试交流群。

</section>

全部评论

(0) 回帖
加载中...
话题 回帖

推荐话题

相关热帖

近期热帖

历年真题 真题热练榜 24小时
技术(软件)/信息技术类
查看全部

近期精华帖

热门推荐