题解 | #字符串排序#

字符串排序

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

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

//链表挂着的哈希表,需要一次遍历原数组,一次遍历哈希表格即可;甚至不需要额外空间
int main() {
    string str;
    getline(cin,str);
    
    vector <list<int>> hash_vec(26);
 
    for(auto ch:str){
        if(isupper(ch))
            hash_vec[ch-'A'].push_back(1);
        else if(islower(ch))
            hash_vec[ch-'a'].push_back(0);
    }
    auto stb=str.begin();
    for(auto lst=hash_vec.begin(); lst!=hash_vec.end(); lst++){
        if (lst->empty()) continue;

        for(auto it:*lst){
            while(!isalpha(*stb) && stb != str.end())
                stb++;
            if(stb == str.end()) break; // 防止超出范围
            if(it)
                *stb='A'+(lst-hash_vec.begin());
            else
                *stb='a'+(lst-hash_vec.begin());
            stb++;
        }
    }

    cout<<str<<endl;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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