题解 | #字符串排序#

字符串排序

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

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

int main(){
    vector<string> tmp(26);
    string input;
    getline(cin,input);
    // 记录每个字母的大小顺序
    for(const auto& ch:input){
        if(ch>='a'&&ch<='z')   
            tmp[ch-'a'].push_back(ch);
        if(ch>='A'&&ch<='Z')
            tmp[ch-'A'].push_back(ch);
    }
    // p1指针,记录当前遍历到的input的位置input[p1]
    int p1 = 0;
    string res;
    // 先把tmp数组倒空
    for(const auto& it:tmp){
        for(int i=0; i<it.size(); i++){
            if((input[p1]>='a'&&input[p1]<='z')||(input[p1]>='A'&&input[p1]<='Z'))
                res += it[i];
            else{
                res += input[p1];
                i--;
            }
            p1++;
        }
    }
    // 如果input是以非字母结尾,res会短一截,要补齐
    for(int i=res.size(); i<input.size(); i++)
        res += input[i];
    cout << res << endl;
}

全部评论

相关推荐

菠落蜜:这个是系统自动投的,不是hr主动打招呼。更抽象的还有ai回复
我的秋招日记
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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