题解 | #在字符串中找出连续最长的数字串#
在字符串中找出连续最长的数字串
https://www.nowcoder.com/practice/2c81f88ecd5a4cc395b5308a99afbbec
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNext()) { String str = in.nextLine(); List<String> res = new ArrayList<>(); StringBuilder stringBuilder = new StringBuilder(); int sum = 0; for (char b : str.toCharArray()) { if (b >= '0' && b <= '9') { stringBuilder.append(b); } else if ((b < '0' || b > '9') && stringBuilder.length() != 0) { res.add(String.valueOf(stringBuilder)); sum = Math.max(sum, stringBuilder.length()); stringBuilder.setLength(0); } } if (stringBuilder.length() != 0) { res.add(String.valueOf(stringBuilder)); sum = Math.max(sum, stringBuilder.length()); } for (String b : res) { if (b.length() == sum)System.out.print(b); } System.out.println("," + sum); } } }