62,614
社区成员
发帖
与我相关
我的任务
分享
public static void main(String[] args) {
String s = "{1,2,{3,4,5},{2,3},4,{3,{4,5},5},3,{1,2,{2,3},4,3},6}";
s = s.replaceAll("\\{|\\}+", "");
System.out.println(s);
String ss[]=s.split(",");
for (String sa : ss) {
System.out.println(sa);
}
}
public static void main(String[] args) {
String s = "{1,2,{3,4,5},{2,3},4,{3,{4,5},5},3,{1,2,{2,3},4,3},6}";
String ss = s.replaceAll("\\{|\\}|\\,", "");
int[] n = new int[ss.length()];
for(int i = 0;i<ss.length();i++){
n[i] = Integer.parseInt(ss.substring(i,i+1));
System.out.println(n[i]);
}
}
}
public static void main(String[] args) {
//数据一
// String s = "{1,2,{3,4,5},{2,3},4,{3,{4,5},5},3,{1,2,{2,3},4,3},6}";
//数据二
String s = "{1,2,{{2,3},3,4,5,{2,3}},{2,3},4,{3,{4,5},5},3,{1,2,{2,3},4,3},6}";
s = s.substring(1, s.length() - 1);
System.out.println(Arrays.asList(s.split(",")));
parseArr(check(s.split(",")));
}
private static void parseArr(List<String> sou) {
List<String> list = new ArrayList<String>();
StringBuffer sb = new StringBuffer();
int len = 0;// {}匹配
boolean b = false;// 用来判断前面是否出线{},
boolean lenBoo = false;// 用来判断是否出现{}
boolean isLast = false;// 用来判断添加,
for (int i = 0; i < sou.size(); i++) {
String s = sou.get(i);
if (s.indexOf("{") > -1) {
b = true;
lenBoo = true;
len++;
}
if (s.indexOf("}") > -1) {
lenBoo = true;
len--;
}
if (len > 0) {
lenBoo = true;
}
if (len == 0 && lenBoo) {
isLast = true;
}
if (lenBoo) {
sb.append(s);
if (!isLast) {
sb.append(",");
isLast = false;
}
lenBoo = false;
}
// 不在括号里面或括号已经匹配成功
if (len == 0) {
// 如果前面是否出现{}
if (!b) {
list.add(s);
} else {
list.add(sb.toString().replaceAll("\\{,\\{","{{").replaceAll("\\},\\}", "}}"));
}
sb.delete(0, sb.length());
b = false;
isLast = false;
}
// System.out.print(list);
}
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i) + " ");
}
}
private static List<String> check(String[] arr) {
List<String> list = new ArrayList<String>();
for (int i = 0; i < arr.length; i++) {
String s = arr[i];
if (s.startsWith("{{")) {
list.add("{");
list.add(s.substring(1, s.length()));
continue;
}
if (s.endsWith("}}")) {
list.add(s.substring(0, s.length()-1));
list.add("}");
continue;
}
list.add(s);
}
return list;
}