Java 为什么这段代码在hackerrank上显示运行时错误
这里给出了一个问题-。下面的代码显示了提交时的运行时错误,我无法找出哪里出了问题Java 为什么这段代码在hackerrank上显示运行时错误,java,data-structures,stack,operators,brackets,Java,Data Structures,Stack,Operators,Brackets,这里给出了一个问题-。下面的代码显示了提交时的运行时错误,我无法找出哪里出了问题 import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); for(int a0 = 0; a0 < t; a0++){ Stac
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for(int a0 = 0; a0 < t; a0++){
Stack stack=new Stack();
int j=0;
String s = in.next();
for(int i=0;i<s.length();i++){
if(s.charAt(i)=='('){
stack.push(s.charAt(i));
}
else if(s.charAt(i)=='['){
stack.push(s.charAt(i));
}
else if(s.charAt(i)=='{'){
stack.push(s.charAt(i));
}
else if(s.charAt(i)==')'){
char str=stack.pop();
if(str=='(')continue;
else j=1;
}
else if(s.charAt(i)==']'){
char str=stack.pop();
if(str=='[')continue;
else j=1;
}
else if(s.charAt(i)=='}'){
char str=stack.pop();
if(str=='{')continue;
else j=1;
}
}
if(stack.isEmpty()==false)System.out.println("NO");
else if(j==1)System.out.println("NO");
else if(j==0)System.out.println("YES");
}
}
private static class Stack{
Node first;
private class Node{
char data;
Node next;
}
public boolean isEmpty(){
return (first==null);
}
public void push(char x){
Node old=first;
first=new Node();
first.data=x;
first.next=old;
}
public char pop(){
char ch=first.data;
first=first.next;
return ch;
}
public int peek(){
return first.data;
}
}
}
import java.util.Scanner;
公共类解决方案{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(系统输入);
int t=in.nextInt();
对于(int a0=0;a0 对于(int i=0;iHint:我们需要的不仅仅是“运行时错误”来帮助。您尝试过自己运行它吗?发生了什么?您的方法peek()
返回int
,但是数据是char
。感谢GhostCat,我找到了它。感谢IQV,我纠正了这一点,现在代码已成功提交。