题解 | #在字符串中找出连续最长的数字串#
在字符串中找出连续最长的数字串
http://www.nowcoder.com/practice/2c81f88ecd5a4cc395b5308a99afbbec
dp解,简单直接
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextLine()) {
String str = sc.nextLine();
int len = str.length();
int[] dp = new int[len+1];
int res = 0;
for (int i =1; i <= len; i++) {
char tmp = str.charAt(i-1);
if (tmp >= '0' && tmp <= '9') {
dp[i] = dp[i-1] +1;
res = Math.max(res, dp[i]);
}
}
for (int i =1; i<= len; i++) {
if (dp[i] == res) {
System.out.print(str.substring(i - res, i));
}
}
System.out.println("," + res);
}
}
}