题解 | 字符串排序

字符串排序

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

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

string myfun(string& str){
    int len=str.size();
    string tempStr;
    for(int i = 0; i<26; i++){
        for(int j = 0; j<len; j++){
            if(str[j]-65 == i || str[j]-'a'== i){
                tempStr.push_back(str[j]);
            }
        }
    }
    for(int i = 0,k=0; i<len&&k<tempStr.size(); i++){
        //限制多肯定能提高或保持效率
        if(str[i]>=65&&str[i]<=90 || str[i]>=97&&str[i]<='z' ){
            str[i]=tempStr[k];
            k++;
        }
    }
    return str;
}


int main() {
   string a;
   getline(cin,a);
   cout<<myfun(a);



}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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