package com.duoduo.day329;
/**
* 如何消除嵌套的括号 并判断正确性
* @author 多多
*
*/
public class Testchange_str {
public static void main(String [] args) {
String s="(1,(2,3),(4,(5,6),7))";
String result=changeStr(s);
if(result!=null)
System.out.println(result);
String s2="((1,3,),(4,5)";
String result1=changeStr(s2);
if(result1!=null)
System.out.println(result1);
}
private static String changeStr(String s) {
String result="(";
char[] c=s.toCharArray();
int count=0; //括号个数
int i=0; //遍历指针
while(i<c.length) {
if(c[i]=='(') { //有左括号 就加1
count++;
}else if(c[i]==')') { //右括号 -1 但是需要判断是否>0
if(count>0)
count--;
else {
System.out.println("expression wrong!");
return null;
}
}else if(c[i]==',') { //逗号 这里就正
嵌套括号匹配问题/不使用比较运算求出两个数的最大值和最小值问题
最新推荐文章于 2022-07-23 23:06:03 发布