题解 | #字符串排序#
字符串排序
https://www.nowcoder.com/practice/5190a1db6f4f4ddb92fd9c365c944584
// HJ26【2】 字符串排序.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 #include<iostream> #include<bits/stdc++.h> using namespace std; class Solution { public: void StrSort(string s); }; void Solution::StrSort(string s) { if (s.empty()) return; vector<char>vec; for (int i = 0; i < 26; i++) { for (int j = 0; j < s.size(); j++) { if (s[j] - 'a' == i || s[j] - 'A' == i) { vec.push_back(s[j]); } } } for (int i = 0,k=0; i < s.size()&&k<vec.size(); i++) { if (isalpha(s[i])) { s[i] = vec[k++]; } } cout << s << endl; } int main() { Solution a; string s; while (getline(cin,s)) { a.StrSort(s); } return 0; }