[635. Design Log Storage System] C++ Unordered_map


  • 0
     class LogSystem {
    unordered_map<string, int> levels = {{"Year", 3}, {"Month", 6}, {"Day", 9}, {"Hour", 12}, {"Minute", 15}, {"Second", 18}};
    unordered_map<string, vector<int> > IDs;
    
    public:
    LogSystem() {}
    void put(int id, string timestamp) {
        IDs[timestamp].push_back(id);
    }
    
    vector<int> retrieve(string s, string e, string gra) {
        vector<int> res;
        for(auto m : IDs){
            bool check = true;
            string str = m.first;
            int n = levels[gra];
            int i = 0;
            while(i <= n){
                int ss = 0;
                int mm = 0;
                while(i <= n && s[i] != ':'){
                    ss = ss*10 + s[i] - '0';
                    mm = mm*10 + str[i] - '0';
                    i++;
                }
                i++;
                if(mm < ss){
                    check = false; break;
                }else if(mm > ss){
                    break;
                }
            }
            i = 0;
            while(check && i <= n){
                int ee = 0;
                int mm = 0;
                while(i <= n && e[i] != ':'){
                    ee = ee*10 + e[i] - '0';
                    mm = mm*10 + str[i] - '0';
                    i++;
                }
                i++;
                if(mm > ee){
                    check = false; break;
                }else if(mm < ee){
                    break;
                }            
            }
            if(check){
                res.insert(res.end(), m.second.begin(), m.second.end());
            }
        }
        return res;
    }
    };

Log in to reply
 

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