欢迎光临散文网 会员登陆 & 注册

???

2023-06-23 23:22 作者:furiousiTy  | 我要投稿

#include <iostream>

#include <stack>

#include <string>


std::string removeDuplicates(const std::string& input) {

    std::stack<char> stack;


    for (char c : input) {

        if (isdigit(c)) { // 判断字符是否是数字

            if (stack.empty() || stack.top() != c) {

                stack.push(c);

            }

        }

    }


    std::string result;

    while (!stack.empty()) {

        result = stack.top() + result;

        stack.pop();

    }


    return result;

}


int main() {

    std::string input = "a1b22c333d4444e55555";

    std::string result = removeDuplicates(input);

    std::cout << "Original string: " << input << std::endl;

    std::cout << "String after removing duplicates: " << result << std::endl;


    return 0;

}


???的评论 (共 条)

分享到微博请遵守国家法律