Java写题解的第6天 | #在字符串中找出连续最长的数字串#
在字符串中找出连续最长的数字串
http://www.nowcoder.com/practice/2c81f88ecd5a4cc395b5308a99afbbec
解题思路:模拟
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String s = ""; while ((s = br.readLine()) != null) { List<String> list = new ArrayList<>(); int maxLen = 0; int left = 0; while (left < s.length()) { if (Character.isDigit(s.charAt(left))) { StringBuilder sb = new StringBuilder(); while (left < s.length() && Character.isDigit(s.charAt(left))) { sb.append(s.charAt(left)); left++; } if (sb.length() == maxLen) { list.add(sb.toString()); } else if (sb.length() > maxLen) { list.clear(); maxLen = sb.length(); list.add(sb.toString()); } } else { left++; } } StringBuilder sb2 = new StringBuilder(); for (int i = 0; i < list.size(); i++) { sb2.append(list.get(i)); } System.out.println(sb2.toString() + "," + list.get(0).length()); } } }