题解 | #字符串排序#

字符串排序

https://www.nowcoder.com/practice/5190a1db6f4f4ddb92fd9c365c944584

#include <algorithm>
#include <vector>
#include <iostream>
#include <string>
using namespace std;


string string_sort(string text) {
    vector<char> alpha_chars;
    for (auto c : text) {
        if (isalpha(c)) {
            alpha_chars.push_back(c);
        }  
    }
    stable_sort(alpha_chars.begin(), alpha_chars.end(), [](auto c1, auto c2){
        return tolower(c1) < tolower(c2);
    });
    size_t index = 0;
    for (size_t i = 0; i < text.size(); ++i) {
        if (isalpha(text[i])) {
            text[i] = alpha_chars[index++];
        }
    }
    return text;
}

int main() {
    string text;
    while (getline(cin, text)) {
        cout << string_sort(text) << endl;
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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