java - 对我的括号的改进,重新对齐 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);
}
}
解决方案
当给定字符串的任何部分(从索引 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
}
推荐阅读
- android - 单击edittext时,我的miui/mobile主题崩溃了
- sql-server - 查看具有多个键列的表,并将其对应的值列放入对应的键列中
- flutter - Flutter Bloc 如何从 Widget 本身更新 BlocBuilder 中的 Widget?
- python - Python中的格式问题
- reactjs - React:加载图像之前的图像大小问题
- java - Viewpager2打开所选页面(来自cardview)没有片段
- r - 基于类似称为列中的值的新列的属性值
- javascript - 如何从函数范围访问数组
- php - 目标类 [App\Http\Controllers\Admin\Category\CategoryController] 不存在
- python - Pandas 数据框搜索和修改邮政编码的前导零