Clean Java - 169ms


  • 0
    A
    public class LogSystem {
        Map<Integer, String> hm;
        public LogSystem() {
            hm = new HashMap<Integer, String>();
        }
        
        public void put(int id, String timestamp) {
            hm.put(id, timestamp);
        }
        
        public List<Integer> retrieve(String s, String e, String gra) {
            List<Integer> res = new ArrayList<>();
            for(int i: hm.keySet()){
                if(gra.equals("Year")){
                    String t = hm.get(i).substring(0,4);
                    if(t.compareTo(s.substring(0,4))>=0 && t.compareTo(e.substring(0,4))<=0)
                        res.add(i);
                }
                if(gra.equals("Month")){
                    String t = hm.get(i).substring(0,7);
                    if(t.compareTo(s.substring(0,7))>=0 && t.compareTo(e.substring(0,7))<=0)
                        res.add(i);
                }
                if(gra.equals("Day")){
                    String t = hm.get(i).substring(0,10);
                    if(t.compareTo(s.substring(0,10))>=0 && t.compareTo(e.substring(0,10))<=0)
                        res.add(i);
                }
                if(gra.equals("Hour")){
                    String t = hm.get(i).substring(0,13);
                    if(t.compareTo(s.substring(0,13))>=0 && t.compareTo(e.substring(0,13))<=0)
                        res.add(i);
                }
                if(gra.equals("Minute")){
                    String t = hm.get(i).substring(0,16);
                    if(t.compareTo(s.substring(0,16))>=0 && t.compareTo(e.substring(0,16))<=0)
                        res.add(i);
                }
                if(gra.equals("Second")){
                    String t = hm.get(i);
                    if(t.compareTo(s)>=0 && t.compareTo(e)<=0)
                        res.add(i);
                }          
            }
            return res;
        }
    }

Log in to reply
 

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