简体   繁体   English

在Java中使用堆栈进行平衡的符号检查

[英]Balanced Symbol Check using Stack in Java

Hey guys so I have an assignment where Im supposed to read a string that the user inputs and check for balanced symbols using a stack. 大家好,我给我分配了一个任务,我应该读取用户输入的字符串,并使用堆栈检查是否有平衡的符号。 So if the string is "{[()]}" the string is balanced because there is a close for every opening. 因此,如果字符串为“ {{(()]}”),则该字符串是平衡的,因为每个开孔都有一个闭合。 My thought is to use a loop that checks every character from the string that is given and if the string has an opener such as "([{" then it does stack.push(char) and if the character is a closer ")]}" then I need to use stack.pop(char). 我的想法是使用一个循环,该循环检查给定字符串中的每个字符,并且该字符串是否具有诸如“ [[{”的开头,则它确实具有stack.push(char)以及该字符是否更接近“)]。 }”,那么我需要使用stack.pop(char)。 The issue im running into is that my professor is forcing me to use a string method and any help ive found online is using a boolean method, id appreciate it if someone could help me out here. 我遇到的问题是,我的教授强迫我使用字符串方法,而在网上找到的任何帮助我都在使用布尔方法,如果有人可以在这里帮助我,我将不胜感激。

I understand that my code is not working but you can at least get the idea of what my logic is. 我知道我的代码无法正常工作,但是您至少可以了解我的逻辑是什么。

import java.util.*; 导入java.util。*;

public class BalancedSymbols { 公共类BalancedSymbols {

public static String balancedSymbols(String lineToCheck){ //this is the method that im being forced to use

    Stack<String> stack = new Stack<String>();

   for (int i = 0; i<lineToCheck.length(); i++){

        char x = '(';
        char y = '{';
        char z = '[';

        char a;
        a = lineToCheck.charAt(i);

        if (a == x){

            stack.push(a);

        }

        if (a == y){

            stack.push(a);

        }

        if (a == z){

            stack.push(a);

        }


    }

}

} }

Obviously I would do the same for popping except with different symbols. 显然,除了使用不同的符号外,我会针对弹出操作执行相同的操作。

Here is the logic: 这是逻辑:

Create empty stack. 创建空堆栈。 (you already have done it). (您已经完成了)。

Traverse through the String. 遍历字符串。

For every character ch you encounter, if ch is } ] ) and stack is empty return false 对于您遇到的每个字符ch ,如果ch是}])并且堆栈为空,则返回false
else 其他

if it is ( { [ push it in stack 如果是({[ 推入 堆栈
if ch is ) } ] check stack top . 如果ch)}] 检查 堆栈顶部
if stack top is equal to corresponding opening bracket pop from stack else return false . 如果堆栈顶部等于从堆栈弹出的对应的开括号,否则返回false
If you have reached to end of String and stack is not empty return false 如果您已到达String的末尾并且堆栈不为空,则返回false
else return true . 否则返回true
The reason behind returning false is we don't have corresponding matching parenthesis for this. 返回false的原因是我们没有与此匹配的括号。 These are extra brackets . 这些是多余的括号

Try to implement it on your own first. 尝试先自己实施。 If you face any problem, comment it.. I'll be happy to help. 如果您遇到任何问题,请对其进行评论。我很乐意为您提供帮助。

Please comment if you have any questions. 如有任何疑问,请发表评论。

 Create empty stack
 ==========================

private static boolean isValideEx(String str) {

    Stack<Character> st=new Stack<Character>();

    if(str == null || str.length() == 0)
        return true;

    for (int i = 0; i < str.length(); i++) {
        if(str.charAt(i)==')'){
                if(!st.isEmpty() && st.peek()=='('){
                    st.pop();
                }else{
                    return false;
                }
        }else if(str.charAt(i)==']'){
                if(!st.isEmpty() && st.peek()=='['){
                    st.pop();
                }else{
                    return false;
                }
        }else if(str.charAt(i)=='}'){
                if(!st.isEmpty() && st.peek()=='{'){
                    st.pop();
                }else{
                    return false;
                }
        }else{
            st.push(str.charAt(i));
        }

    }
    System.out.println(" sy "+st);

    if(st.isEmpty())
        return true;
    else
        return false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM