题目链接
https://www.nowcoder.com/questionTerminal/bd891093881d4ddf9e56e7cc8416562d
题目
读入一个字符串str,输出字符串str中的连续最长的数字串
输入描述:
个测试输入包含1个测试用例,一个字符串str,长度不超过255。
输出描述:
在一行内输出str中里连续最长的数字串。
示例1
输入
abcd12345ed125ss123456789
输出
123456789
代码
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (input.hasNextLine()) {
String str = input.nextLine();
int max = 0;
int end = 0;
int count = 0;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (Character.isDigit(c)) {
count++;
if (max < count) {
max = count;
end = i;
}
} else {
count = 0;
}
}
System.out.println(str.substring(end - max + 1, end + 1));
}
input.close();
}
}