题解 | #表示数字#

表示数字

https://www.nowcoder.com/practice/637062df51674de8ba464e792d1a0ac6

import java.util.Scanner;

/**
 * @author hll[yellowdradra@foxmail.com]
 * @since 2023-03-30 23:16
 **/
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();
        StringBuilder sb = new StringBuilder();
        boolean start = false;
        for (char c : str.toCharArray()) {
            // 遇到了数字 还没开始 即数字的开始
            if (isDigit(c) && !start) {
                sb.append("*").append(c);
                start = true;
                continue;
            }
            // 遇到了非数字 已经开始了 即数字的结束
            if (!isDigit(c) && start) {
                sb.append("*").append(c);
                start = false;
                continue;
            }
            // 数字进行中或非数字进行中
            sb.append(c);
        }
        // 如果是以数字结尾的还要最后补*
        boolean isEndWithDigit = isDigit(str.charAt(str.length() - 1));
        System.out.println(sb.append(isEndWithDigit ? "*" : ""));
    }

    public static boolean isDigit(char c) {
        return c <= '9' && c >= '0';
    }
}

全部评论

相关推荐

03-26 22:55
门头沟学院 Java
烤冷面在迎接:河南byd,应该就是郑大了。不过24届计算机是特殊情况,那年除了九✌和强2,以及两三个关系够硬的双非,其他的都是炮灰,感觉是十几年来互联网行业最烂的一年,如果想了解最新的就业情况,得找现在的大四。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务