leetcode 937(未完)

class Solution {
public:
    static bool cmp(string a, string b){
        int key1 = getkey(a);
        int key2 = getkey(b);
        if(a.substr(key1) == b.substr(key2)){
            //cout<
            return a.substr(0,key1) < b.substr(0,key2);
        }
        return a.substr(key1) < b.substr(key2);
    }
    vector<string> reorderLogFiles(vector<string>& logs) {
        //sort(logs.begin(), logs.end(), cmp);
        vector<string> digs;
        vector<string> lets;
        for(string s : logs){
            // if(s.substr(0,3) == "dig"){
            if(!islet(s)){
                digs.push_back(s);
            }else{
                lets.push_back(s);
            }
        }
        sort(lets.begin(), lets.end(), cmp);
        vector<string> ans;
        for(string s : lets){
            ans.push_back(s);
        }
        for(string s : digs){
            ans.push_back(s);
        }
        return ans;
    }
    bool islet(string a){
        int index = 0;
        while(a[index] != ' '){
            index ++;
        }
        int num = a[index+1] - '0';
        if(num >= 0 && num <= 9){
            return false;
        }
        return true;
    }
    static int getkey(string s){
        int index = 0;
        while(s[index] != ' '){
            index ++;
        }
        //return s.substr(0,index);
        return index;
    }
};

你可能感兴趣的:(leetcode,leetcode,算法,职场和发展)