题解 | #字符串中找出连续最长的数字串#
字符串中找出连续最长的数字串
https://www.nowcoder.com/practice/bd891093881d4ddf9e56e7cc8416562d
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String str = in.nextLine();
char c1 [] = str.toCharArray();//将所读到的字符串转化字符数组
ArrayList list = new ArrayList();//用于数字串的添加,可以不知道数字串的数目
for(int i=0;i<c1.length;i++){//
String str1="";
if(c1[i]>='0' && c1[i]<='9'){//如果识别到第一个数字
str1=str1+c1[i]+"";
for(int j=i+1;j<c1.length;j++){//重第一个数字的后一位开始看是否满足为数字
if(c1[j]>='0' && c1[j]<='9'){//如果是数字就添加到字符串中
str1=str1+c1[j]+"";
}
if(!(c1[j]>='0' && c1[j]<='9') || j==c1.length-1){//(1)如果数字不是跳出,(2)会有一种特殊情况,最后一位是数字但是要跳出
if(j ==c1.length-1 && (c1[j]>='0' && c1[j]<='9') ){//最后一位是数字
str1=str1+c1[j]+"";
}
i=j--;//不要指针回溯,跳出后重j为开始,所以给i赋值
list.add(str1);//将字符串添加进集合中
break;
}
}
}
}
String s2="";
for(Object o:list){//利用增强for去循环
String s3 = (String) o;
if(s2.length()<s3.length()){//如果有比s2更长的数字串更新s2
s2=s3.substring(0,s3.length());
}
}
System.out.printf("%s",s2);
}
}

影石Insta360公司氛围 452人发布