微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

测试用例“[}}]”无法验证用java编码的表达式平衡括号问题

如何解决测试用例“[}}]”无法验证用java编码的表达式平衡括号问题

这段代码是在我试图解决 Leet 代码上的问题时编写的(下面给出了问题的链接),它执行平衡括号,但条件 ([}}]) 失败,任何人都可以帮助我。

谢谢。

问题链接---> https://leetcode.com/problems/valid-parentheses/

import java.util.*;

public class expressionValidation 
{
    public static void main(String[] args)
 {
            try (Scanner sc = new Scanner(system.in))/*trying to avoid any kind of exceptions*/
 {
                String str = sc.nextLine();
                String exp = "";/*new string  to modify the given expression*/
                int l = str.length();

                for(int i=0;i<l;i++)
{
                    if(str.charat(i)=='{'||str.charat(i)=='('||str.charat(i)=='['||str.charat(i)=='}'||str.charat(i)==']'||str.charat(i)==')')
{
                        exp+=str.substring(i,i+1);/*newly modified string afterstrong text removing everything except brackets'(' '[' '{' ' }' ']' ')'*/
                    }
                }   
                stack ob = new stack();
                System.out.println(ob.isValid(exp)?"Balanced":"NOT Balanced");
            }
        
    }
}
## The following is the stack class
class stack
{
    boolean isValid(String exp)
{
        int l =exp.length();
         if(l%2!=0)
                return false;
         Stack<Character> st = new Stack<Character>();
         for(int i=0;i<l;i++) 
{
             if(exp.charat(i)=='{' ||exp.charat(i)=='(' ||exp.charat(i)=='[' ) {
                 st.push(exp.charat(i));
             }
             else if(exp.charat(i)=='}' && !(st.isEmpty()) && st.peek()=='{') {
                 st.pop();
             }
             else if(exp.charat(i)==')' && !(st.isEmpty()) && st.peek()=='(') {
                 st.pop();
             }
             else if(exp.charat(i)==']' && !(st.isEmpty()) && st.peek()=='[') {
                 st.pop();
             }
             String str = st.toString();
             System.out.println(str);
         }
        return st.isEmpty();
    }

}

解决方法

试试这个。

CreateStudentCommand
,

当您遇到}并且以下条件失败时

else if(exp.charAt(i)=='}' && !(st.isEmpty()) && st.peek()=='{') {

您应该已经产生了错误,但您只是默默地忽略传入的 } 并继续迭代。因此,所有未配对的右括号/方括号都会从您的输入中默默删除。您分析的不是 ([}}]),而是 ([]),它是一个平衡字符串,因此您不会出错。

其他结束字符也是如此。

,

我提交的 LeetCode :-

public boolean isValid(String s) {
    Stack<Character> sc = new Stack<>();
    for(int i =0;i<s.length();i++){
        char ch = s.charAt(i);
        if(ch == '(' || ch == '{' || ch == '['){
            sc.push(ch);
        }else if(ch == ')'){
            if(!sc.isEmpty() && sc.peek() == '('){
                sc.pop();
            }else{
                return false;
            }
        }else if(ch == '}'){
            if(!sc.isEmpty() && sc.peek() == '{'){
                sc.pop();
            }else{
                return false;
            }
        }else if(ch == ']'){
            if(!sc.isEmpty() && sc.peek() == '['){
                sc.pop();
            }else{
                return false;
            }
        }
    }
    if(sc.isEmpty()){
        return true;
    }else{
        return false;
    }
}

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。