题解 | #在字符串中找出连续最长的数字串#
在字符串中找出连续最长的数字串
http://www.nowcoder.com/practice/2c81f88ecd5a4cc395b5308a99afbbec
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextLine()) {
String str = sc.nextLine();
char[] chars = str.toCharArray();
ArrayList<String> list = new ArrayList<>();
int end = 0;
for (int i = 0; i < chars.length; ) {
if (Character.isDigit(chars[i])) {
if(i == chars.length-1){
list.add(str.substring(i));
break;
}
for (int j = i + 1; j < chars.length; j++) {
if(j == chars.length -1){
end = j + 1;
}
if (!Character.isDigit(chars[j])) {
end = j;
break;
}
}
list.add(str.substring(i, end));
i = end;
}else{
i++;
}
}
int max = 0;
for (int i = 0; i < list.size(); i++) {
max = Math.max(list.get(i).length(), max);
}
for (int i = 0; i < list.size(); i++) {
if (list.get(i).length() == max) {
System.out.print(list.get(i));
}
}
System.out.println("," + max);
}
}
}