题解 | #字符串排序#

字符串排序

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

#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
#include <unordered_map>
#include <map>
#include <algorithm>

bool cmp(const string& a,const string& b){
    int size = min(a.size(),b.size());
    for(int i = 0;i<size;i++){
        if(a[i] == b[i])
            continue;
        else
            return a[i] < b[i];
    }
    return a.size() < b.size() ? true : false;
}

int main() {
    int n;
    cin >> n;
    vector<string> a;
    for(int i = 0;i<n;i++){
        string t;
        cin >> t;
        a.push_back(t);
    }
    sort(a.begin(),a.end(),cmp);

    for(auto x:a)
        cout << x << endl;
}

重写了cmp方法,考虑空字符串,虽然默认就是字典排序

全部评论

相关推荐

人间雪:简历最好只要一页,除非你牛逼到一页都写不下了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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