【无标题】

重新排列日志文件

#include
#include
#include
using namespace std;
bool cmp(const string a,const string b) {
    int aNumberCount = 0;
    string tmpa = "";
    string tmpb = "";
    string titleA = "";
    string titleB = "";
    int flag = 0;
    for (int i=0;i= '0' && a[i] <= '9') {
            aNumberCount++;
        }
        if (flag == 1) {
            tmpa += a[i];
        }else {
            titleA += a[i];
        }
    }
    int bNumberCount = 0; flag = 0;
    for (int i=0;i= '0' && b[i] <= '9') {
            bNumberCount++;
        }
        if (flag == 1) {
            tmpb += b[i];
        }else {
            titleB += b[i];
        }
    }
    if (aNumberCount > 0 && bNumberCount ==0) return false;
    if (aNumberCount == 0 && bNumberCount > 0) return true;
    if(aNumberCount>0&&bNumberCount>0) return false;
    if (tmpa == tmpb) {
        return titleA < titleB;
    }
    return tmpa < tmpb;
}
class Solution {
public:
    vector reorderLogFiles(vector& logs) {
        stable_sort(logs.begin(),logs.end(),cmp);
        return logs;
    }
}; 

int main() {
    Solution s=Solution();
    vectorcases;
    cases.push_back("l5sh 6 3869 08 1295");
    cases.push_back("16o 94884717383724 9");
    cases.push_back("43 490972281212 3 51");
    cases.push_back("9 ehyjki ngcoobi mi");
    cases.push_back("2epy 85881033085988");
    cases.push_back("7z fqkbxxqfks f y dg");
    cases.push_back("9h4p 5 791738 954209");
    cases.push_back("p i hz uubk id s m l");
    cases.push_back("wd lfqgmu pvklkdp u");
    cases.push_back("m4jl 225084707500464");
    cases.push_back("6np2 bqrrqt q vtap h");
    cases.push_back("e mpgfn bfkylg zewmg");
    cases.push_back("ttzoz 035658365825 9");
    cases.push_back("k5pkn 88312912782538");
    cases.push_back("ry9 8231674347096 00");
    cases.push_back("w 831 74626 07 353 9");
    cases.push_back("bxao armngjllmvqwn q");
    cases.push_back("0uoj 9 8896814034171");
    cases.push_back("0 81650258784962331");
    cases.push_back("t3df gjjn nxbrryos b");
    vectorans = s.reorderLogFiles(cases);
    for (int i=0;i

重新定义下比较方式就好,注意不能使用快排,不稳定。学习到了C++新的库函数-stable_sort。原理是并归。

你可能感兴趣的:(重启常文正,程序人生)