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

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

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

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.stream.Collectors;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        while (in.hasNextLine()) {

            String string = in.nextLine();

            List<SubString> subStringList = getSubStringList(string);

            LinkedHashMap<Integer, List<SubString>> map = subStringList
                    .stream()
                    .filter(subString -> isDigitString(subString.getString()))
                    .sorted(Comparator.comparing(SubString::getLength).reversed().thenComparing(
                                SubString::getIndex))
                    .collect(Collectors.groupingBy(SubString::getLength, LinkedHashMap::new,
                                                   Collectors.toList()));

            System.out.println(toString(map.entrySet().iterator().next()));
        }

        in.close();
    }

    public static String toString(Entry<Integer, List<SubString>> entry) {
        return entry.getValue()
               .stream()
               .map(SubString::getString)
               .collect(Collectors.joining(""))
               + "," + entry.getKey();
    }

    public static List<SubString> getSubStringList(String string) {
        int length = string.length();

        List<SubString> subStringList = new ArrayList<>();

        for (int i = 1; i <= length; i++) {
            for (int j = 0; j <= length - i; j++) {
                String substring = string.substring(j, j + i);
                subStringList.add(new SubString(substring.length(), substring, j));
            }
        }
        return subStringList;
    }

    public static boolean isDigitString(String s) {
        boolean digitString = true;
        try {
            new BigInteger(s);
        } catch (NumberFormatException ex) {
            digitString = false;
        }
        return digitString;
    }

    public static String reverse(String s) {
        char[] chars = s.toCharArray();
        int length = s.length();
        StringBuilder builder = new StringBuilder();
        for (int i = length - 1; i >= 0; i--) {
            builder.append(chars[i]);
        }
        return builder.toString();
    }

    public static class SubString {
        private int length;
        private String string;
        private int index;

        public SubString(int length, String string, int index) {
            this.length = length;
            this.string = string;
            this.index = index;
        }

        public int getLength() {
            return length;
        }

        public void setLength(int length) {
            this.length = length;
        }

        public String getString() {
            return string;
        }

        public void setString(String string) {
            this.string = string;
        }

        public int getIndex() {
            return index;
        }

        public void setIndex(int index) {
            this.index = index;
        }
    }

}

全部评论

相关推荐

LZStarV:冲就好了,就算真的是字节也冲,面评脏了大不了等三四个月就淡了,而且等到那个时候实力进步了选择还多,何必拘泥于字节
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务