Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
851 views
in Technique[技术] by (71.8m points)

implement a method in java to check correctness

i have an assessment here,and i provided a solution but when i run the code,the result is weird. Here is the assessment: "You are given a sequence of characters consisting of parentheses ( ) and brackets [ ].

A String of this type is said to be correct:

  • if it is an empty or null string
  • if the string A is correct ,(A) and [A] are correct
  • if the string A and B are correct the concatenation AB is also correct

input: The string contains at most 10000 characters.

Examples:[( )] is correct ,(( )[ ]) is correct,( [ ) ] is not correct,(( is not correct.

Implement the method check(String str) to check the correctness of a string of this type. check returns true if the string is correct,false otherwise. "

So i did that code below:

public class Solution {
    
    public static boolean check(String str) {
         String str1 = null,str2 = null;
         if(str==null|| str.length()==0) return true;
         if (check(str1)==true && check(str2)==true) {
             return true;
         }
        return false;
    }
    public static void main(String[] args) {
        
        System.out.println(Solution.check("([])"));    //true
        System.out.println(Solution.check("()[]"));    //true
        System.out.println(Solution.check("([)]"));    //false
        System.out.println(Solution.check("(("));      //false
        System.out.println(Solution.check("[(()])"));   //false

    }

}

But i run it and i get:

true
true
true
true
true

How can i fix it?

question from:https://stackoverflow.com/questions/65832198/implement-a-method-in-java-to-check-correctness

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
import java.util.Stack;

public class HelloWorld {
    public static void main(String []args) {
        System.out.println(check("([])"));    //true
        System.out.println(check("()[]"));    //true
        System.out.println(check("([)]"));    //false
        System.out.println(check("(("));      //false
        System.out.println(check("[(()])"));   //false
         System.out.println(check("([(([]))][]())"));   //true
    }
    
    public static boolean check(String v) {
        if (v == null || v.isEmpty()) return true;
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < v.length(); ++i) {
            char c = v.charAt(i);
            if (c == '(' || c == '[') {
                stack.push(c);
            } else if (stack.isEmpty()) {
                return false;
            } else if ((stack.peek() == '(' && c == ')') || (stack.peek() == '[' && c == ']')) {
                stack.pop();
            } else {
                return false;
            }
        }
        return stack.isEmpty();
    }
}

Use stack is right.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...