题解 | #字符串排序#哈希表 简洁易懂代码

字符串排序

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

#include <iostream>
#include <vector>
#include <map>
using namespace std;

int main()
{
    map<char, string> hash;
    string str;
    getline(cin, str);
    for(int i = 0; i < str.size(); i++)
        if(isalpha(str[i]))
            hash[tolower(str[i])].push_back(str[i]);
    string sortStr;
    for(auto it : hash)
        sortStr += it.second;
    int j = 0;
    for(int i = 0; i < str.size(); i++)
        if(isalpha(str[i]))
            str[i] = sortStr[j++];
    cout << str << endl;    
    return 0;
}
全部评论

相关推荐

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