有效括号 空栈异常

huangapple go评论136阅读模式
英文:

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 &lt;stringSize; x++)
{
switch (s.charAt(x)){
case &#39;(&#39;:
stack1.push(x);
break;
case &#39;{&#39;:
stack1.push(x);
break;
case &#39;[&#39;:
stack1.push(x);
break;
case &#39;)&#39;:{
if(stack1.empty()){
g=false;
return false;}
else if (stack1.pop()==&quot;(&quot;){
break;}
else if (stack1.pop()==&quot;{&quot;){
g=false;
return false;
}
else if (stack1.pop()==&quot;[&quot;){
g=false;
return false;
}
}
case &#39;}&#39;:{
if(stack1.empty()){
g=false;
return false;}
else if (stack1.pop()==&quot;{&quot;){
break;}
else if (stack1.pop()==&quot;(&quot;){
g=false;
return false;
}
else if (stack1.pop()==&quot;[&quot;){
g=false;
return false;
}
}
case &#39;]&#39;:{
if(stack1.empty()){
g=false;
return false;}
else if (stack1.pop()==&quot;[&quot;){
break;}
else if (stack1.pop()==&quot;(&quot;){
g=false;
return false;
}
else if (stack1.pop()==&quot;{&quot;){
g=false;
return false;
}
}
}
}
return g;
}
}

答案1

得分: 2

在各个地方使用s.charAt(x)而不是x: 链接至Java官方文档中的String.charAt(int)

英文:

Use s.charAt(x) instead of x in various places: https://docs.oracle.com/en/java/javase/20/docs/api/java.base/java/lang/String.html#charAt(int)

huangapple
  • 本文由 发表于 2023年4月10日 23:27:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75978408.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定