题解 | #字符串排序#

字符串排序

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

参考评论区题解

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

void sort_str(string& str) {
    vector<vector<char>> v(26);
    for (char c: str) {
        if (!isalpha(c)) continue;
        if (c >= 'a') v[c-'a'].push_back(c);
        else v[c-'A'].push_back(c);
    }
    int k = 0;
    for (int i = 0; i < 26; i++) {
        for (int j = 0; j < v[i].size(); j++) {
            while (!isalpha(str[k])) k++;
            str[k++] = v[i][j];
        }
    }
}

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

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务