题解 | #字符串排序#

字符串排序

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")

全部评论

相关推荐

05-26 10:24
门头沟学院 Java
qq乃乃好喝到咩噗茶:其实是对的,线上面试容易被人当野怪刷了
点赞 评论 收藏
分享
AAA专业长城贴瓷砖刘大爷:这样的简历我会直接丢进垃圾桶,花里胡哨的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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