题解 | #在字符串中找出连续最长的数字串#
在字符串中找出连续最长的数字串
https://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.hasNextLine()){
String input = sc.nextLine();
StringBuffer sb = new StringBuffer();
for (char c : input.toCharArray()) {
if(Character.isDigit(c)){
sb.append(1);
} else {
sb.append(0);
}
}
String[] nums = sb.toString().split("0");
int maxLength = 0;
for (String num : nums) {
maxLength = Math.max(num.length(), maxLength);
}
StringBuffer result = new StringBuffer();
for(int i = 0; i <= input.length() - maxLength; ++i){
boolean isOk = true;
for(int j = i; j < i + maxLength; ++j){
if(!Character.isDigit(input.charAt(j))){
isOk = false;
break;
}
}
if(isOk){
result.append(input.substring(i, i + maxLength));
}
}
System.out.println(result.toString()+","+maxLength);
}
}
}
