题解 | #表示数字#

表示数字

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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String a;
        try {
            a = in.readLine();
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        int i = 0, j = 0, l = a.length(), numCnt = 0;
        char[] chrAy = new char[l];
        char[] transfer = new char[2 * l];
        a.getChars(0, l, chrAy, 0);
        while (i < l) {
            if ((chrAy[i] - '0' | '9' - chrAy[i]) > 0) {
                numCnt++;
                if (numCnt == 1)//第一位数字前面加*
                    transfer[j++] = '*';
                transfer[j++] = chrAy[i];
                if (i == l - 1)//特殊情况,最后一位是数字,后面加*
                    transfer[j++] = '*';
            } else {
                if (numCnt > 0) {//前面是数字,当前位不是数字,加*
                    transfer[j++] = '*';
                    numCnt = 0;
                }
                transfer[j++] = chrAy[i];
            }
            i++;
        }
        System.out.print(new String(transfer, 0, j));
    }

}

全部评论

相关推荐

点赞 评论 收藏
分享
可以不说话:笔试a了3道半,今天说是挂了😭😭
投递汇丰科技等公司8个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务