Peek()如何工作?

发布于 2025-02-10 06:07:22 字数 1360 浏览 1 评论 0原文

import java.util.Scanner;
import java.util.Stack;

// All 3 queries (1:push, 2:delete, 3:print max) are all O(1) runtime
public class Solution {
    public static void main(String[] args) {
        Stack<Integer> stack    = new Stack<Integer>();
        Stack<Integer> maxStack = new Stack<Integer>(); // keeps track of maximums

        Scanner scan = new Scanner(System.in);
        int N = scan.nextInt();
        for (int i = 0; i < N; i++) {
            int query = scan.nextInt();
            switch (query) {
                case 1:
                    int x = scan.nextInt();
                    stack.push(x);
                    if (maxStack.isEmpty() || x >= maxStack.peek()) {
                        maxStack.push(x);
                    }
                    break;
                case 2:
                    int poppedValue = stack.pop();
                    if (poppedValue == maxStack.peek()) {
                        maxStack.pop();
                    }
                    break;
                case 3:
                    System.out.println(maxStack.peek());
                    break;
                default:
                    break;
}
        }        
        scan.close();
    }
}

这是在堆栈中查找最大值的代码,我想知道,如果peek()选择堆栈的顶部元素,那么如果有一个元素大于最高值,则该元素的窥视方式堆栈。如何确定最大值

import java.util.Scanner;
import java.util.Stack;

// All 3 queries (1:push, 2:delete, 3:print max) are all O(1) runtime
public class Solution {
    public static void main(String[] args) {
        Stack<Integer> stack    = new Stack<Integer>();
        Stack<Integer> maxStack = new Stack<Integer>(); // keeps track of maximums

        Scanner scan = new Scanner(System.in);
        int N = scan.nextInt();
        for (int i = 0; i < N; i++) {
            int query = scan.nextInt();
            switch (query) {
                case 1:
                    int x = scan.nextInt();
                    stack.push(x);
                    if (maxStack.isEmpty() || x >= maxStack.peek()) {
                        maxStack.push(x);
                    }
                    break;
                case 2:
                    int poppedValue = stack.pop();
                    if (poppedValue == maxStack.peek()) {
                        maxStack.pop();
                    }
                    break;
                case 3:
                    System.out.println(maxStack.peek());
                    break;
                default:
                    break;
}
        }        
        scan.close();
    }
}

This is code for finding max value in the stack i want to know that if peek() chooses the top element of the stack than how it will peek if there is an element greater than top value like in between the stack. how it will determine the max value

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝咒 2025-02-17 06:07:22

使用您的情况(最多之间),它可以正常工作:
如果您在stack中具有值:1,4,5,3,2,1 - 然后。并执行案例2:将留下stack带有1,2,5,3,2MaxStack不会被弹出,所以仍然有5个作为顶级元素。

但是,使用的顺序1,2,3,5,5删除查询将从stackmaxstack中执行POP(),它导致:

stack - &gt; 1,2,3,5

maxstack - &gt; 1,2,3

我认为这是逻辑中的缺陷。

With you case (max in between) it works fine:
If you have values in the stack: 1,4,5,3,2,1 - then maxStack will contain 5 as the top element. And executing case 2: will leave stack with 1,2,5,3,2 and maxStack will not be popped so still have 5 as top element.

However with the sequence of 1,2,3,5,5 delete query will execute pop() from both stack and maxStack which leads to:

stack -> 1,2,3,5

maxStack -> 1,2,3

Which I believe is a flaw in the logic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文