题解 | #字符串排序#

字符串排序

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

#队列 + 数组队列,逻辑清晰

#include <iostream>
#include <limits>
#include <queue>    
#include <vector>
using namespace std;

int main() {
    string s;
    getline(cin,s);
    queue<int> q;
    vector<queue<char>> vec(26);

    for(int i=0;i<s.size();++i)
    {
        int num = s[i] - 'A';
        int num1 = s[i] - 'a';
        if(num >=0 && num <26)
        {
            q.emplace(i);
            vec[num].emplace(s[i]);
        }
        if(num1 >= 0 && num1 <26)
        {
            q.emplace(i);
            vec[num1].emplace(s[i]);
        }
    }

    while(!q.empty())
    {
        for(int i=0;i<26;++i)
        {
            while(!vec[i].empty())
            {
                int idx = q.front();
                char c = vec[i].front();
                q.pop();
                vec[i].pop();
                s[idx] = c;
            }
        }
    }

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

全部评论

相关推荐

不愿透露姓名的神秘牛友
06-10 15:24
高考前一晚在OPPO手机上设置了早上5:30的闹钟,然而闹钟并未按时响起。直到妈妈做好早餐后,在6:27打开手机才发现闹钟未触发,“气得早上饭都没吃”。资本家你赢了
永不遗忘:我来解释一下 :Oppo 手机晚上两点会自动进行系统更新,这个系统更新会重置掉所有设置好的闹钟,而且他也不会告诉你,而且只有 Oppo 会这样,华为苹果小米三星都不会
点赞 评论 收藏
分享
爱吃肉的伊登在写日记:好棒,27届简历能做成这个样子,但是第一个项目感觉cover住难度还是不小的,特别是二面的时候肯定要对分布式系统设计这一块儿有高出正常面试者的水平才行
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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