Concise Java Solution


  • 31
    public class LogSystem {
        
        List<String[]> timestamps = new LinkedList<>();
        List<String> units = Arrays.asList("Year", "Month", "Day", "Hour", "Minute", "Second");
        int[] indices = new int[]{4,7,10,13,16,19};
        
        public void put(int id, String timestamp) { timestamps.add(new String[]{Integer.toString(id), timestamp}); }
    
        public List<Integer> retrieve(String s, String e, String gra) {
            List<Integer> res = new LinkedList<>();
            int idx = indices[units.indexOf(gra)];
            for (String[] timestamp : timestamps) {
                if (timestamp[1].substring(0, idx).compareTo(s.substring(0, idx)) >= 0 &&
                   	timestamp[1].substring(0, idx).compareTo(e.substring(0, idx)) <= 0) res.add(Integer.parseInt(timestamp[0]));
            }
            return res;
        }
    }
    

  • 1
    C

    For compareTo method, there is a link

    The comparison is based on the Unicode value of each character in the strings.


  • 0

    Very brilliant solution making use of String.compareTo() method!


  • 0
    B

    @chenx376 so?


  • 0

    Same idea in c++:

    class LogSystem {
    private:
        vector<pair<string, int>> store;
        unordered_map<string, int> idx;
    
    public:
        LogSystem() {
            idx = { {"Year", 4}, {"Month", 7}, {"Day", 10}, {"Hour", 13}, {"Minute", 16}, {"Second", 19}};
        }
        
        void put(int id, string timestamp) {
            store.emplace_back(timestamp, id);
        }
        
        vector<int> retrieve(string s, string e, string gra) {
            vector<int> res;
            int len = idx[gra];
            for (auto &p : store) {
                auto t = p.first.substr(0, len);
                if (s.substr(0, len) <= t && t <= e.substr(0, len))
                    res.push_back(p.second);
            }
            return res;
        }
    };
    

  • 2
    D

    In fact we can use Map to store the unit and index pairs:

    public class LogSystem {
    
        private List<String[]> db;
        Map<String, Integer> unitIdxMap;
        
        public LogSystem() {
            db = new ArrayList<>();
            unitIdxMap = new HashMap<>();
            // init
            unitIdxMap.put("Year", 4);
            unitIdxMap.put("Month", 7);
            unitIdxMap.put("Day", 10);
            unitIdxMap.put("Hour", 13);
            unitIdxMap.put("Minute", 16);
            unitIdxMap.put("Second", 19);
        }
        
        public void put(int id, String timestamp) {
            db.add(new String[]{String.valueOf(id), timestamp});
        }
        
        public List<Integer> retrieve(String s, String e, String gra) {
            
            int idx = unitIdxMap.get(gra);
            List<Integer> res = new ArrayList<>();
            for (String[] st : db)
            {
                if (st[1].substring(0, idx).compareTo(s.substring(0, idx)) >= 0 
                    && st[1].substring(0, idx).compareTo(e.substring(0, idx)) <= 0)
                {
                    res.add(Integer.valueOf(st[0]));
                }
            }
            
            return res;
        }
    }
    

  • 0
    I

    @dongll Then why not replacing db from list to map too?


  • 0
    K

    The time complexity every time is N


Log in to reply
 

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