首页 > 解决方案 > 对我的括号的改进,重新对齐 Java 代码

问题描述

这是我的作业代码。任务是以括号' 后跟 E 的形式获取用户输入来标记其结尾并确定它是否是正确的序列 [例如 (()())E 是正确的,)(() ((E 不正确] 如果不正确,请确定您需要在开头删除多少括号以及在末尾添加多少括号才能正确。

该代码有效。但它超长,我想知道是否可以对其进行任何改进。任何帮助是极大的赞赏。

public class Main {
  
  public static String enter(){     
    String enter = "";
    char nextValue ='A';
    while (nextValue!='E'){
      nextValue = In.readChar();
      enter += nextValue;
    }
    return enter;
  }
// User Input. I know this could be done with ArrayList or scanner, but we haven't had these yet in classes. 

//So I did it by converting the user inputs into a string, 

//then storing that string into an array in main with "char []sequence = enter.toCharArray();"


  public static char[] removeParenthesisPairs(char[] a){
    for(int i=0; i<a.length; i++){
      if(a[i]=='('){
        a[i]='F';
        outerloop:
        for(int j=i+1; j<a.length;j++){
          if(a[j]==')'){
            a[j]='F';
            break outerloop;
          }
          if(j==a.length-1){
            a[i]='(';
          }
        }
      }
    }
    return a;
  }
// I remove all the correct pairs from the sequence. I look for a ( and turn it into an F [to signal it's removed] 

//and search the rest of the sequence for a ) and turn that into an F too. If there is no ) I turn the F back into (.

//This part I really don't like, it looks super clunky. Also I have read that the "break outerloop"

//thing I'm using here is bad practice, but I havent figured out any other way to accomplish the same result.
  

  
  public static boolean checkIfCorrect(char[] a){
    for(int i=0;i<a.length-1;i++){
      if(a[i]!='F'){
        return false;
      }
    }
    return true;
  }
// I check if the sequence is correct. If there are only F left in the array, 

//that means all entries were able to form pairs of () and the original sequence was correctly parenthesised.
  
  
   public static int subtract(char[] a){
    int count = 0;
    for(int i=0; i<a.length;i++){
      if(a[i]==')'){
        count += 1;
      }
    }
    return  count;
  }
// Check how many ) have to be subtracted at the beginning of the sequence.
  

  public static int add(char[] a){
    int count = 0;
    for(int i=0; i<a.length;i++){
      if(a[i]=='('){
        count += 1;
      }
    }
    return count;
  }
// Check how many ( are still left in the sequence and need a ) added to form a pair.
  
  public static void main(String[] args) {
    String enter= enter();
    char []sequence = enter.toCharArray();
    removeParenthesisPairs(sequence);
    boolean correct = checkIfCorrect(sequence);
    int additional = add(sequence);
    int removed = subtract(sequence);
    Out.println(correct);
    Out.println("removed = " + removed + " und additional = " + additional);
  }
}

标签: java

解决方案


当给定字符串的任何部分(从索引 0 到任何索引)中“(”的数量小于“)”的数量时,序列不正确。所以......只要有一个“计数”,当你遇到“(”时加一,当遇到“)”时减去1:

    public static boolean works(String str) {//The string I meant here is like "()()()(())" or ")()()()(()))((())". Not "(())(((((E"
        int cnt = 0;//count the number of "(" appear and ")" appear
        
        for(int i = 0;i<str.length();i++) {
            if(str.charAt(i) =='(') {
                cnt++;//add 1
            }else {
                cnt--;//subtract 1
            }
            
            if(cnt < 0) {//There is more ")"s than "("s
                return false;//Doesn't work;
            }
        }
        return cnt == 0;//In case the numbers of "(" appear and the numbers ")" appear are different
    }

推荐阅读