描述:
输入一个string,找出里面1-9的连续数字,例如输入112365478,输出123,78
知识点:char转化为int类型:char-'0'即可。
@org.junit.Test
public void test() {
//输入一个string,找出里面1-9的连续数字,例如输入112365478,输出123,78
String number = "1123654789";
char[] chars = number.toCharArray();
ArrayList<ArrayList<Integer>> list = new ArrayList<>();
int index = 0;
while (index < chars.length - 1) {
if ((chars[index + 1] - '0') - (chars[index] - '0') != 1) {
index++;
} else if ((chars[index + 1] - '0') - (chars[index] - '0') == 1) {
ArrayList<Integer> list1 = new ArrayList<>();
int i = index;
list1.add(chars[i] - '0');
while (i + 1 < chars.length && (chars[i + 1] - '0') - (chars[i] - '0') == 1) {
list1.add(chars[i + 1] - '0');
i++;
}
index = i;
list.add(list1);
}
}
System.out.println("list:" + TestJsonUtils.marshalToString(list));
}