题解 | #表示数字#

表示数字

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

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        char[] ch = str.toCharArray();
        String[] arrstr = new String[ch.length];
        String[] res = new String[ch.length];
        for (int i = 0; i < ch.length; i++) {
            arrstr[i] = Character.toString(ch[i]);
            res[i] = Character.toString(ch[i]);
        }
        if (arrstr[0].matches("[0-9]") && arrstr[1].matches("[0-9]")) {
            res[0] = "*" + arrstr[0];
        }
        if (arrstr[0].matches("[0-9]") && arrstr[1].matches("[^0-9]")) {
            res[0] = "*" + arrstr[0] + "*";
        }
        if (arrstr[ch.length - 1].matches("[0-9]") &&
                arrstr[ch.length - 2].matches("[0-9]")) {
            res[ch.length - 1] = arrstr[ch.length - 1] + "*";
        }
        if (arrstr[ch.length - 1].matches("[0-9]") &&
                arrstr[ch.length - 2].matches("[^0-9]")) {
            res[ch.length - 1] = "*" + arrstr[ch.length - 1] + "*";
        }
        for (int i = 1; i < ch.length - 1; i++) {
            if (arrstr[i].matches("[0-9]") && arrstr[i - 1].matches("[^0-9]") &&
                    arrstr[i + 1].matches("[^0-9]")) {
                res[i] = "*" + arrstr[i] + "*";
            }
            if (arrstr[i].matches("[0-9]") && arrstr[i - 1].matches("[^0-9]") &&
                    arrstr[i + 1].matches("[0-9]")) {
                res[i] = "*" + arrstr[i];
            }
            if (arrstr[i].matches("[0-9]") && arrstr[i - 1].matches("[0-9]") &&
                    arrstr[i + 1].matches("[^0-9]")) {
                res[i] = arrstr[i] + "*";
            }
        }

        for (String s : res) {
            System.out.print(s);
        }
    }
}

全部评论

相关推荐

在笔试的大西瓜很矫健:校招数分不用想了,这经历和学历都不够用,大厂更别想,初筛都过不了,说点不好听的小厂数分都进不去(小厂也是假数分),要两个对口实习+3个项目(或者3+2),而且要有含金量才能补一点你的学历劣势。 建议刷实习,社招找数分,校招看运气,能入行业就行,可以运营转数分
点赞 评论 收藏
分享
01-19 12:48
门头沟学院 C++
只想搞钱的鸽子很喜欢...:混账是很多的,还有那些在自己风华正茂的年纪说风凉话讥讽那些下岗前员工的。这些人都是现在职场环境这么烂的帮凶
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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