Simple & fast C++ solution


  • 0
    B
    class LogSystem {
        map<string, int> logs;
        
    public:
        LogSystem() {
            
        }
        
        void put(int id, string timestamp) {
            logs[timestamp] = id;
        }
        
        vector<int> retrieve(string s, string e, string gra) {
            static string sGra[] = {
                "Year", "Month", "Day", "Hour", "Minute", "Second"            
            };
            static string sLowerFiller = ":01:01:00:00:00";
            static string sUpperFiller = ":12:32:23:59:59";
            
            int fillerPos = 4;
            for (int i = 0; i < sizeof(sGra) / sizeof(sGra[0]); i++) {
                if (gra == sGra[i]) {
                    copy(sLowerFiller.begin() + i * 3, sLowerFiller.end(), s.begin() + fillerPos);
                    copy(sUpperFiller.begin() + i * 3, sUpperFiller.end(), e.begin() + fillerPos);
                    break;
                }
                fillerPos += 3;
            }
            
            vector<int> res;
            
            auto it = logs.lower_bound(s), itEnd = logs.upper_bound(e);
            while (it != itEnd) {
                res.push_back(it->second);
                ++it;
            }
    
            return res;
        }
    };
    

Log in to reply
 

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