题解 | #在字符串中找出连续最长的数字串#
在字符串中找出连续最长的数字串
http://www.nowcoder.com/practice/2c81f88ecd5a4cc395b5308a99afbbec
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String str = sc.nextLine();
int maxLen = 0;
int len = 0;
//int index = 0;
int len2 = 0;
for(int i = 0; i < str.length(); i++){
if((str.charAt(i)+"").matches("[0-9]")){
len++;
}else if((str.charAt(i)+"").matches("[^0-9]")){
if(maxLen < len){
maxLen = len;
//index = i;
}
len = 0;
}else{}
if(maxLen < len){
maxLen = len;
//index = i;
}
}
for(int i = 0; i < str.length(); i++){
if((str.charAt(i)+"").matches("[0-9]")){
len2++;
if(len2 == maxLen){
System.out.print(str.substring(i+1-maxLen, i+1));
len2 = 0;
}
}else if((str.charAt(i)+"").matches("[^0-9]")){
len2 = 0;
}else{}
}
System.out.println(","+maxLen);
}
}
}