英文:
Valid Parenthesis Empty Stack Exception
问题
I am trying to solve the problem statement at Valid Parenthesis
I have been editing and modifying for a while and needed help. Thank you all for the initial help on the syntax issue.
The error I'm getting is for the input case of s = "()"; my code returns the Empty Stack Exception instead of just "True"
java.util.EmptyStackException
at line 101, java.base/java.util.Stack.peek
at line 83, java.base/java.util.Stack.pop
at line 37, Solution.isValid
at line 54, __DriverSolution__.__helper__
at line 84, __Driver__.main
THANK YOU FOR ANY HELP!!!
import java.util.*;
class Solution {
Stack stack1 = new Stack();
public boolean isValid(String s) {
int stringSize = s.length();
boolean g = true;
for (int x = 0; x < stringSize; x++) {
switch (s.charAt(x)) {
case '(':
stack1.push(x);
break;
case '{':
stack1.push(x);
break;
case '[':
stack1.push(x);
break;
case ')': {
if (stack1.empty()) {
g = false;
return false;
} else if (stack1.pop() == "(") {
break;
} else if (stack1.pop() == "{") {
g = false;
return false;
} else if (stack1.pop() == "[") {
g = false;
return false;
}
}
case '}': {
if (stack1.empty()) {
g = false;
return false;
} else if (stack1.pop() == "{") {
break;
} else if (stack1.pop() == "(") {
g = false;
return false;
} else if (stack1.pop() == "[") {
g = false;
return false;
}
}
case ']': {
if (stack1.empty()) {
g = false;
return false;
} else if (stack1.pop() == "[") {
break;
} else if (stack1.pop() == "(") {
g = false;
return false;
} else if (stack1.pop() == "{") {
g = false;
return false;
}
}
}
}
return g;
}
}
英文:
I am trying to solve the problem statement at Valid Parenthesis
I have been editing and modifying for a while and needed help. Thank you all for the initial help on the syntax issue.
The error I'm getting is for the input case of s ="()" my code returns the Empty Stack Exception instead of just "True"
java.util.EmptyStackException
at line 101, java.base/java.util.Stack.peek
at line 83, java.base/java.util.Stack.pop
at line 37, Solution.isValid
at line 54, __DriverSolution__.__helper__
at line 84, __Driver__.main
THANK YOU FOR ANY HELP!!!
import java.util.*;
class Solution {
Stack stack1 = new Stack();
public boolean isValid(String s) {
int stringSize= s.length();
boolean g = true;
for (int x=0; x <stringSize; x++)
{
switch (s.charAt(x)){
case '(':
stack1.push(x);
break;
case '{':
stack1.push(x);
break;
case '[':
stack1.push(x);
break;
case ')':{
if(stack1.empty()){
g=false;
return false;}
else if (stack1.pop()=="("){
break;}
else if (stack1.pop()=="{"){
g=false;
return false;
}
else if (stack1.pop()=="["){
g=false;
return false;
}
}
case '}':{
if(stack1.empty()){
g=false;
return false;}
else if (stack1.pop()=="{"){
break;}
else if (stack1.pop()=="("){
g=false;
return false;
}
else if (stack1.pop()=="["){
g=false;
return false;
}
}
case ']':{
if(stack1.empty()){
g=false;
return false;}
else if (stack1.pop()=="["){
break;}
else if (stack1.pop()=="("){
g=false;
return false;
}
else if (stack1.pop()=="{"){
g=false;
return false;
}
}
}
}
return g;
}
}
答案1
得分: 2
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论