java堆栈中的最大元素

java堆栈中的最大元素,java,algorithm,performance,stack,max,Java,Algorithm,Performance,Stack,Max,ques将执行三个查询:1-用于将elment添加到堆栈2-用于poping元素3-用于打印max元素 链接张贴在上面 我无法在hackerrank上获得6-7个案例的输出 int n,i;int in1,in2; Scanner sc=new Scanner(System.in); n=sc.nextInt(); Stack<Integer> st=new Stack<Integer>();//stack for elements

ques将执行三个查询:1-用于将elment添加到堆栈2-用于poping元素3-用于打印max元素 链接张贴在上面 我无法在hackerrank上获得6-7个案例的输出

    int n,i;int in1,in2;
    Scanner sc=new Scanner(System.in);
    n=sc.nextInt();
    Stack<Integer> st=new Stack<Integer>();//stack for elements
    Stack<Integer> stmax=new Stack<Integer>(); // stack for storing maximum
        for(i=1;i<=n;i++)
        {
        in1=sc.nextInt();
        if(in1==1)
            {
                     in2=sc.nextInt();
                     if(st.size()==0)
                       {

                              stmax.push(in2);
                              st.push(in2);
                       }
                     else
                         {
                              if(in2>=stmax.peek())
                                  {

                              stmax.push(in2);
                              }
                               st.push(in2);

                          }    
        }
        if(in1==2)
            {
            if(st.peek()==stmax.peek())
                stmax.pop();
            st.pop();

        }
        if(in1==3)
            {
            System.out.println(stmax.peek());
        }

        }
intn,i;int-in1,in2;
扫描仪sc=新的扫描仪(System.in);
n=sc.nextInt();
Stack st=新堆栈()//元素堆栈
Stack stmax=new Stack();//用于存储最大容量的堆栈
对于(i=1;i=stmax.peek())
{
stmax.push(in2);
}
圣普什(in2);
}    
}
如果(in1==2)
{
if(st.peek()==stmax.peek())
stmax.pop();
圣普();
}
如果(in1==3)
{
System.out.println(stmax.peek());
}
}

您的代码是正确的,但不要使用
if(st.peek()==stmax.peek())
使用
if(st.peek().equals(stmax.peek())
这将使您的代码通过所有测试用例

什么是“无法获得输出”的意思?@abhinav提问时请具体、简短……评论您的代码,并在发布之前将其设置为IDE格式。使用拼写检查。我无法通过hackerrank上的7个测试用例。链接张贴在上面。感谢ans!它现在正在工作。但是你能解释一下你的解决方案吗?你的堆栈使用整数,整数是和字符串一样的对象,所以==实际上意味着引用相等而不是值相等,你需要值相等