题解 | #字符串排序#

字符串排序

https://www.nowcoder.com/practice/5af18ba2eb45443aa91a11e848aa6723

#include <iostream>
#include <utility>
#include<vector>
#include<algorithm>
using namespace std;

void Qicksort(vector<string>& words,int start,int end){
    if(start >= end)return;
    int xiaoduiend = start,luanduistart = start+1;
    for(;luanduistart <= end;++luanduistart){
        if(words[luanduistart] <= words[start]){
            ++xiaoduiend;
            swap(words[xiaoduiend],words[luanduistart]);
        }
    }
    swap(words[xiaoduiend],words[start]);
    Qicksort(words,start,xiaoduiend-1);
    Qicksort(words, xiaoduiend+1,end); 
}
int main() {
    int n;
    cin >> n;
    vector<string> words(n);
    for(int i = 0;i < n;++i){
        cin >> words[i];
    }
    Qicksort(words, 0, n-1);
    for(int i = 0;i < n;++i){
        cout << words[i] << endl;
    }
}
// 64 位输出请用 printf("%lld")

最好自己写出快排,不用sort

全部评论

相关推荐

不愿透露姓名的神秘牛友
06-11 13:34
offe从四面八方来:我真的没时间陪你闹了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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