• leetcode栈和队列


    1.栈和队列常见方法

    (1)栈:

    1. stack.push()//压栈
    2. stack.pop()//弹栈
    3. stack.empty()==true//栈为空
    4. stack.peek()//返回栈顶元素(只返回不删除)

    (2)队列:

    1. queue.add():往队尾添加元素
    2. queue.remove():删除队头元素(并且返回队头元素)
    3. queue.peek():返回队头元素(只返回不删除)
    4. Queue queue=new LinkedList();
    5. queue.add(1)
    6. queue.add(2)
    7. queue.add(3)
    8. queue.add(4)
    9. queue.add(5)
    10. int size=queue.szie();
    11. for(int i=0;i<size;i++)
    12. {
    13. Systerm.out.println(queue.remove());
    14. //得到1,2,3,4,5
    15. }

    双端队列:

    (1)队列只能在队尾添加元素,在队头删除元素,而双端队列在队头队尾都可以进行添加和删除操作

    (2)双端队列Deque的常见方法:(在队列的add,peek,remove方法基础上进行改造)

    1. //创建双端队列
    2. Deque queue=new LinkedList();
    3. //First表示队首,Last表示队尾
    4. //在队首添加元素
    5. queue.addFirst();
    6. //在队尾增加元素
    7. queue.addLast();
    8. //获取队首元素
    9. queue.peekFirst();
    10. //获取队尾元素
    11. queue.peekLast();
    12. //移除并且返回队首元素
    13. queue.removeFirst();
    14. queue.removeLast();

    2.用两个栈实现队列:力扣(剑指offer09)

    把栈和队列都想象成一个黑箱子

    将1,2,3依次输入到这个黑箱子,如果这个黑箱子是栈,就依次返回3,2,1

                                                         如果这个黑箱子是队列,就依次返回1,2,3

    加入元素的时候直接加入到stack1里面就行

    主要是弹出元素时:弹出元素肯定是弹出stack2中的元素

    如果stack2中有元素,就直接stack2.pop()

    stack2为空的时候(即stack2中没有元素可以弹出的时候),把stack1中的元素全部转移到stack2中,然后再stack2.pop()

     将stack1内的元素全部转移到stack2内 :

    1. class CQueue
    2. {
    3. Stack<Integer> stack1;
    4. Stack<Integer> stack2;
    5. public CQueue()
    6. {
    7. stack1=new Stack();
    8. stack2=new Stack();
    9. }
    10. //往你创建的队列中添加元素
    11. public void appendTail(int value)
    12. {
    13. stack1.push(value);
    14. }
    15. //往你创建的队列中弹出元素
    16. public int deleteHead()
    17. {
    18. //要弹出元素,肯定是弹出stack2这个栈里面的栈顶元素
    19. //如果stack2栈内元素为空,stack1内也没元素,那就返回-1
    20. //如果stack2栈内元素为空,stack1内有元素,就把stack1内所有元素转移到stack2里面去
    21. /*比如stack1里面添加了四个元素1,2,3,4,现在显然是想要打印出1,2,3,4
    22. 所以要把1,2,3,4全部从stack1中压入stack2中,这样不断stack2.pop()才能得到1,2,3,4*/
    23. if(stack2.empty()==true)
    24. {
    25. if(stack1.empty()==true) return -1;
    26. else//如果stack1不为空
    27. {
    28. while(stack1.empty()==false)
    29. {
    30. stack2.push(stack1.pop());
    31. }
    32. }
    33. }
    34. return stack2.pop();
    35. }
    36. }

    1. //创建队列(调用构造方法)
    2. CQueue obj = new CQueue();
    3. //添加一个元素进队列队尾
    4. obj.appendTail(value);
    5. //删除队头元素并且返回这个队头元素
    6. int param_2 = obj.deleteHead();

    另外一道跟这个也很相似力扣(leetcode232)

    1. class MyQueue
    2. {
    3. Stack<Integer> stack1;
    4. Stack<Integer> stack2;
    5. //调用构造方法创建两个栈
    6. public MyQueue()
    7. {
    8. stack1=new Stack();
    9. stack2=new Stack();
    10. }
    11. public void push(int x)
    12. {
    13. stack1.push(x);
    14. }
    15. public int pop()
    16. {
    17. //肯定是弹出stack2中的元素
    18. //stack2为空的时候(即stack2中没有元素可以弹出的时候),把stack1中的元素全部转移到stack2中
    19. if(stack2.empty()==true)
    20. {
    21. while(stack1.empty()==false)
    22. {
    23. stack2.push(stack1.pop());
    24. }
    25. }
    26. return stack2.pop();
    27. }
    28. public int peek()
    29. {
    30. if(stack2.empty()==true)
    31. {
    32. while(stack1.empty()==false)
    33. {
    34. stack2.push(stack1.pop());
    35. }
    36. }
    37. return stack2.peek();
    38. }
    39. public boolean empty()
    40. {
    41. if(stack1.empty()==true&&stack2.empty()==true)
    42. {
    43. return true;
    44. }
    45. return false;
    46. }
    47. }

    3.用两个队列实现栈

    力扣225

    定义两个队列,其中queue1队列用来加入元素,queue2队列用来peek和pop弹出元素

     

    1. class MyStack
    2. {
    3. Queue<Integer> queue1;
    4. Queue<Integer> queue2;
    5. public MyStack()
    6. {
    7. queue1=new LinkedList();
    8. queue2=new LinkedList();
    9. }
    10. public void push(int x)
    11. {
    12. //step1:先把元素添加到queue1里面
    13. queue1.add(x);
    14. //step2:只要队列queue2里面还有元素,就要把它转移到队列queue1里面
    15. while(queue2.size()!=0)
    16. {
    17. queue1.add(queue2.remove());
    18. }
    19. //step3:此时元素就全部在队列queue1里面了,再交换queue1和queue2即可
    20. Queue temp=queue1;
    21. queue1=queue2;
    22. queue2=temp;
    23. }
    24. public int pop()
    25. {
    26. return queue2.remove();
    27. }
    28. public int top()
    29. {
    30. return queue2.peek();
    31. }
    32. public boolean empty()
    33. {
    34. return queue2.size()==0;
    35. }
    36. }

    4.最小栈

    力扣155

    最小栈其实就是在栈的基础上多了一个获取最小元素的功能

    这道题进行测试时

    1. minStack minstack=new MinStack();
    2. minstack(-2);
    3. minstack(0);
    4. ninstack(-3);
    5. //先压三个元素进栈
    6. //然后调用getMin()方法来获取栈内最小元素是多少
    7. minstack.getMin();

     暴力方法:

    1. class MinStack
    2. {
    3. Stack<Integer>stack;
    4. public MinStack()
    5. {
    6. stack=new Stack();
    7. }
    8. public void push(int val)
    9. {
    10. stack.push(val);
    11. }
    12. public void pop()
    13. {
    14. stack.pop();
    15. }
    16. public int top()
    17. {
    18. return stack.peek();
    19. }
    20. public int getMin()
    21. {
    22. ArrayList<Integer> a=new ArrayList();
    23. for(int i:stack)
    24. {
    25. a.add(i);
    26. }
    27. int temp=Integer.MAX_VALUE;
    28. for(int j:a)
    29. {
    30. temp=Math.min(temp,j);
    31. }
    32. return temp;
    33. }

    显然暴力法时间复杂度为O(n),因为遍历栈的时间复杂度为O(n)

    题目规定要用常数时间内找到最小元素肯定是满足不了的

    双栈法:用两个栈,一个栈是正常的栈,元素进栈和出栈都是正常的

    另一个栈是特殊的栈,一个元素要想进这个栈必须小于这个栈的栈顶元素,

    当正常栈弹出栈顶元素时,特殊栈要不要也弹出栈顶元素呢?当正常栈要弹出的栈顶元素等于特殊栈的栈顶元素时,特殊栈的栈顶元素也要弹出,不相等时,特殊栈的栈顶元素就不用弹出

    1. class MinStack
    2. {
    3. Stack<Integer> stack1;
    4. Stack<Integer> stack2;
    5. public MinStack()//初始化两个栈,一个正常栈,一个特殊栈
    6. {
    7. stack1=new Stack();
    8. stack2=new Stack();
    9. }
    10. public void push(int x)
    11. {
    12. //stack1正常栈,入栈时正常入栈就行
    13. stack1.push(x);
    14. if(stack2.empty()) stack2.push(x);
    15. else
    16. {
    17. if(x<=stack2.peek()) stack2.push(x);
    18. }
    19. }
    20. public void pop()
    21. {
    22. int temp=stack1.peek();
    23. stack1.pop();
    24. if(!stack2.empty()&&stack2.peek()==temp) stack2.pop();
    25. }
    26. public int top()
    27. {
    28. return stack1.peek();
    29. }
    30. public int getMin()
    31. {
    32. return stack2.peek();
    33. }
    34. }

  • 相关阅读:
    Java高级特性之多线程,java实战项目视频
    针对CSP-J/S的每日一练:Day 8
    LeetCode 2596. 检查骑士巡视方案
    STL list合并
    基于stm32单片机甲醛烟雾温湿度检测仪设计
    JAVA学习(方法的定义和调用)
    使用VSCode新建解决方案,添加ClassLib类库工程
    Less的强大变量用法
    jenkins2.346.2安装
    jsonXML格式化核心代码
  • 原文地址:https://blog.csdn.net/weixin_47414034/article/details/125457451