Straightforward cpp solution


  • 0
    H
    class LogSystem {
    public:
        unordered_map<int, string> dic;
        unordered_map<string, int> priority;
        LogSystem() {
            dic.clear();
            priority["Year"] = 4;
            priority["Month"] = 7;
            priority["Day"] = 10;
            priority["Hour"] = 13;
            priority["Minute"]=16;
            priority["Second"] =19;
        }
        
        void put(int id, string timestamp) {
         	dic[id] = timestamp;   
        }
     
        int  cmp(string a, string b, string gra){
            int stop = priority[gra];
            return a.substr(0, stop).compare(b.substr(0,stop));
        }
        vector<int> retrieve(string s, string e, string gra) {
            vector<int> res;
            for(auto ts:dic){
                string tt = ts.second;
                if( cmp(tt, s,gra) >=0 && cmp(tt, e,gra) <=0) 
                    res.push_back(ts.first);
            }
            return res;
        }
    
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.