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

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

https://www.nowcoder.com/practice/bd891093881d4ddf9e56e7cc8416562d

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();
        solve(str);
    }
    public static void solve(String s) {
            StringBuffer sb = new StringBuffer(s);
            char[] chr = s.toCharArray();
            for(int i = 0; i < chr.length; i++) {
                if(!Character.isDigit(chr[i])) {
                    sb = sb.replace(i,i + 1, ",");
                }
            }
            String[] str = sb.toString().split(",");
            int index = 0;
            for(int i = 0; i < str.length - 1; i++) {
                if(str[i + 1].length() > str[index].length()) {
                    index = i + 1;
                }
            }
            System.out.println(str[index]);
    }
}

全部评论

相关推荐

点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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