java solution using bst


  • 0
    2
    public class LogSystem {
    
        private TreeMap<String, Integer> tm;
        
        public LogSystem() {
            tm = new TreeMap<>();
        }
        
        public void put(int id, String timestamp) {
            tm.put(timestamp, id);
        }
        
        public List<Integer> retrieve(String s, String e, String gra) {
            int length;
            if (gra.equals("Year")) {
                length = 4;
            } else if (gra.equals("Month")) {
                length = 7;
            } else if (gra.equals("Day")) {
                length = 10;
            } else if (gra.equals("Hour")) {
                length = 13;
            } else if (gra.equals("Minute")) {
                length = 16;
            } else if (gra.equals("Second")) {
                length = 19;
            } else {
                throw new RuntimeException();
            }
            return new ArrayList(tm.subMap(s.substring(0, length), e.substring(0, length) + "\127").values());
        }
    }
    
    /**
     * Your LogSystem object will be instantiated and called as such:
     * LogSystem obj = new LogSystem();
     * obj.put(id,timestamp);
     * List<Integer> param_2 = obj.retrieve(s,e,gra);
     */
    

Log in to reply
 

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