经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 其他 » 职业生涯 » 查看文章
[算法总结] 6 道题搞定 BAT 面试——堆栈和队列
来源:cnblogs  作者:尾尾部落  时间:2018/9/25 19:24:43  对本文有异议

本文首发于我的个人博客:尾尾部落

0. 基础概念

:后进先出(LIFO)

队列:先进先出(FIFO)

1. 栈的 java 实现

  1. import java.util.Arrays;
  2. public class Stack {
  3. private int size = 0; //栈顶位置
  4. private int[] array;
  5. public Stack(){
  6. this(10);
  7. }
  8. public Stack(int init) {
  9. if(init <= 0){
  10. init = 10;
  11. }
  12. array = new int[init];
  13. }
  14. /**
  15. * 入栈操作
  16. * @param item 入栈的元素
  17. */
  18. public void push(int item){
  19. if(size == array.length){
  20. array = Arrays.copyOf(array, size*2); //扩容操作
  21. }
  22. array[size++] = item;
  23. }
  24. /**
  25. * 获取栈顶元素,但栈顶元素不出栈
  26. * @return 栈顶元素
  27. */
  28. public int peek(){
  29. if(size == 0){ //空栈
  30. throw new IndexOutOfBoundsException("栈是空的");
  31. }
  32. return array[size-1];
  33. }
  34. /**
  35. * 出栈,同时获取栈顶元素
  36. * @return
  37. */
  38. public int pop(){
  39. int item = peek(); //获取栈顶元素
  40. size--; //直接使元素个数减1,不用清除元素,下次入栈会覆盖旧元素的值
  41. return item;
  42. }
  43. /**
  44. * 判断栈是否已满
  45. * @return
  46. */
  47. public boolean isFull(){
  48. return size == array.length;
  49. }
  50. /**
  51. * 判断栈是否为空
  52. * @return
  53. */
  54. public boolean isEmpty(){
  55. return size == 0;
  56. }
  57. public int getSize(){
  58. return size;
  59. }
  60. }

2. 队列的 java 实现

  1. public class ArrayQueue {
  2. private final Object[] queue; //声明一个数组
  3. private int head;
  4. private int tail;
  5. /**
  6. * 初始化队列
  7. * @param capacity 队列长度
  8. */
  9. public ArrayQueue(int capacity){
  10. this.queue = new Object[capacity];
  11. }
  12. /**
  13. * 入队
  14. * @param o 入队元素
  15. * @return 入队成功与否
  16. */
  17. public boolean put(Object o){
  18. if(head == (tail+1)%queue.length){
  19. //说明队满
  20. return false;
  21. }
  22. queue[tail] = o;
  23. tail = (tail+1)%queue.length; //tail标记后移一位
  24. return true;
  25. }
  26. /**
  27. * 返回队首元素,但不出队
  28. * @return
  29. */
  30. public Object peak() {
  31. if(head==tail){
  32. //队空
  33. return null;
  34. }
  35. return queue[head];
  36. }
  37. /**
  38. * 出队
  39. * @return 出队元素
  40. */
  41. public Object pull(){
  42. if(head==tail){
  43. return null;
  44. }
  45. Object item = queue[head];
  46. queue[head] = null;
  47. return item;
  48. }
  49. /**
  50. * 判断是否为空
  51. * @return
  52. */
  53. public boolean isEmpty(){
  54. return head == tail;
  55. }
  56. /**
  57. * 判断是否为满
  58. * @return
  59. */
  60. public boolean isFull(){
  61. return head == (tail+1)%queue.length;
  62. }
  63. /**
  64. * 获取队列中的元素个数
  65. * @return
  66. */
  67. public int getsize(){
  68. if(tail>=head){
  69. return tail-head;
  70. }else{
  71. return (tail+queue.length)-head;
  72. }
  73. }
  74. }

3. 用两个栈实现队列

剑指offer:用两个栈实现队列
LeetCode:Implement Queue using Stacks

  1. class MyQueue {
  2. Stack<Integer> input = new Stack<Integer>();
  3. Stack<Integer> output = new Stack<Integer>();
  4. /** Push element x to the back of queue. */
  5. public void push(int x) {
  6. input.push(x);
  7. }
  8. /** Removes the element from in front of queue and returns that element. */
  9. public int pop() {
  10. peek();
  11. return output.pop();
  12. }
  13. /** Get the front element. */
  14. public int peek() {
  15. if(output.isEmpty()){
  16. while(!input.isEmpty())
  17. output.push(input.pop());
  18. }
  19. return output.peek();
  20. }
  21. /** Returns whether the queue is empty. */
  22. public boolean empty() {
  23. return input.isEmpty() && output.isEmpty();
  24. }
  25. }

4. 用队列实现栈

LeetCode:Implement Stack using Queues

  1. class MyStack {
  2. Queue<Integer> q1 = new LinkedList<Integer>();
  3. Queue<Integer> q2 = new LinkedList<Integer>();
  4. /** Push element x onto stack. */
  5. public void push(int x) {
  6. if(q1.isEmpty()){
  7. q1.add(x);
  8. for(int i = 0; i < q2.size(); i++){
  9. q1.add(q2.poll());
  10. }
  11. }else{
  12. q2.add(x);
  13. for(int i = 0; i < q1.size(); i++){
  14. q2.add(q1.poll());
  15. }
  16. }
  17. }
  18. /** Removes the element on top of the stack and returns that element. */
  19. public int pop() {
  20. return q1.isEmpty() ? q2.poll() : q1.poll();
  21. }
  22. /** Get the top element. */
  23. public int top() {
  24. return q1.isEmpty() ? q2.peek() : q1.peek();
  25. }
  26. /** Returns whether the stack is empty. */
  27. public boolean empty() {
  28. return q1.isEmpty() && q2.isEmpty();
  29. }
  30. }

5. 包含min函数的栈

剑指offer:包含min函数的栈
定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数。

  1. class MinStack {
  2. Stack<Integer> stack = new Stack<Integer>();
  3. Stack<Integer> temp = new Stack<Integer>();
  4. public void push(int x) {
  5. stack.push(x);
  6. if(temp.isEmpty() || temp.peek() >= x)
  7. temp.push(x);
  8. }
  9. public void pop() {
  10. int x = stack.pop();
  11. int min = temp.peek();
  12. if(x == min)
  13. temp.pop();
  14. }
  15. public int top() {
  16. return stack.peek();
  17. }
  18. public int getMin() {
  19. return temp.peek();
  20. }
  21. }

6. 栈的压入、弹出序列

剑指offer:栈的压入、弹出序列
输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。(注意:这两个序列的长度是相等的)

  1. import java.util.ArrayList;
  2. import java.util.Stack;
  3. public class Solution {
  4. public boolean IsPopOrder(int [] pushA, int [] popA) {
  5. if(pushA.length != popA.length ||
  6. pushA.length == 0 ||
  7. popA.length == 0)
  8. return false;
  9. Stack<Integer> stack = new Stack<>();
  10. int index = 0;
  11. for(int i = 0; i < pushA.length; i++){
  12. stack.push(pushA[i]);
  13. while(!stack.empty() && stack.peek() == popA[index]){
  14. stack.pop();
  15. index++;
  16. }
  17. }
  18. return stack.empty();
  19. }
  20. }
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号