题解 | #在字符串中找出连续最长的数字串#

在字符串中找出连续最长的数字串

https://www.nowcoder.com/practice/2c81f88ecd5a4cc395b5308a99afbbec

import java.util.Scanner;
import java.util.LinkedList;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) {
            String word = in.nextLine();
            int[] maxArr = new int[word.length()];
            int digitLen = 0;
            int maxLen = 0;
            for (int i = word.length() - 1; i >= 0; i--) {
                if (Character.isDigit(word.charAt(i))) {
                    maxArr[i] = ++digitLen;
                } else {
                    maxLen = Math.max(maxLen, digitLen);
                    digitLen = 0;
                }
            }

            for (int i = 0; i < maxArr.length; i++) {
                if (maxArr[i] == maxLen) {
                    System.out.print(word.substring(i, i + maxArr[i]));
                }
            }
            System.out.print(',');
            System.out.println(maxLen);
        }
    }
}

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务