“今天开始讲和大家坚持打卡面试非常重要算法练习---剑指offer,希望我们能一起肝。
- <section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;">
栈 栈是一种先进后出的数据结构。栈中的元素从栈顶(top)压入(push),从栈顶弹出(top)。注意:压入我们需要判栈是否已满,弹出需要判栈是否已空。下面我们看看图加深下印象。 入栈 1 2 3 出栈 3 2 1
</section> - <section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;">
队列 队列是一种先进先出的数据结构。队列中的元素从队尾(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-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;">相信大家已经知道了栈是先进后出,队列是先进先出。假设1 2 3进栈,第一次出栈为3 2 1(反了一次),但是我们想要的出来的顺序为1 2 3,那么我们在此基础再反一次。 </section>
- <section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;">ok,其实我们需要引入另一栈来辅助我们完成,如下图。 </section>
3 动画演示
“不好意思哈,因为支持视频只支持b站,腾讯视频,可是我还没上传,我就放我公众号的外链吧。 动画演示
3 代码实现
- <section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;">c++版本 </section>
- <section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;">python版本 </section>
- <section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;">java版本 </section>
5 唠嗑
“</section>2020年6月29日打卡,打卡格式"打卡XX天"。暖蓝汇聚大家一起,探讨简历修改,面试经历分享,尽全力让大家能在2020找到理想的工作。如果你想加入,加我拉你进面试交流群。
全部评论
(0) 回帖