My easy,fast and simple solution.


  • 0
    B

    ...
    class LogSystem {
    vector<pair<int,string>>data;
    public:
    LogSystem() {
    data.clear();
    }

    	void put(int id, string timestamp) {
    		data.push_back({id,timestamp});
    	}
    
    	vector<int> retrieve(string s, string e, string gra) {
    		int len=0;
    		if(gra=="Year")
    			len=4;
    		else if(gra=="Month")
    			len=7;
    		else if(gra=="Day")
    			len=10;
    		else if(gra=="Hour")
    			len=13;
    		else if(gra=="Minute")
    			len=16;
    		else
    			len=19;
    		vector<int>ans;
    		string ss=s.substr(0,len);
    		string es=e.substr(0,len);
    		for(int i=0; i<data.size(); ++i) {
    			string substr=data[i].second.substr(0,len);
    			if(substr>=ss&&substr<=es)
    				ans.push_back(data[i].first);
    		}
    		return ans;
    	}
    

    };
    ...


Log in to reply
 

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