题解 | #表示数字#

表示数字

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);
        }
    }
}

全部评论

相关推荐

07-29 14:46
门头沟学院 Java
码农索隆:好了,我说句公道话,咱三都辛苦了
点赞 评论 收藏
分享
能干的三文鱼刷了10...:公司可能有弄嵌入式需要会画pcb的需求,而且pcb能快速直观看出一个人某方面的实力。看看是否有面试资格。问你问题也能ai出来,pcb这东西能作假概率不高
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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