华为机试HJ96题解 | #表示数字#

表示数字

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

#include <cctype>
#include <iostream>
//#include <string>
#include <bits/stdc++.h>
using namespace std;

// 双指针解法
string AddStartsBeforeAndAfterDigital(const std::string &s)
{
    string res;
    // 遍历字符串
    for (int i = 0; i < s.size(); ) {
        int j = i;
        // 当前字符是数字
        if (isdigit(s[j])) {
            // 找到最后一个数字(连续数字)
            while (isdigit(s[j]) && j < s.size()) {
                j++;
            }
            // 在连续数字前后加上*
            res += "*" + s.substr(i, j - i) + "*";
            // 记录最后一个数字的下一个字符
            i = j;
        } else {
            // 如果当前字符不是数字,则直接加上
            res += s[j];
            i++;
        }
    }

    return res;
}

int main() {
    string s;
    while (cin >> s) { // 注意 while 处理多个 case
        cout << AddStartsBeforeAndAfterDigital(s) << endl;
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

黑皮白袜臭脚体育生:简历统一按使用了什么技术实现了什么功能解决了什么问题或提升了什么性能指标来写会更好另外宣传下自己的开源仿b站微服务项目,GitHub已经410star,牛客上有完整文档教程,如果觉得有帮助的话可以点个小星星,蟹蟹
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务