Short Python via comparing tuples


  • 1

    Here I convert timestamps into tuples and used a dictionary self.record to record them. When retrieving, just comparing the tuples by first idx elements.

    class LogSystem(object):
    
        def __init__(self):
            self.record = {}
    
        def put(self, id, timestamp):
            """
            :type id: int
            :type timestamp: str
            :rtype: void
            """
            self.record[id] = tuple(map(int, timestamp.split(':')))
            
        def retrieve(self, s, e, gra):
            """
            :type s: str
            :type e: str
            :type gra: str
            :rtype: List[int]
            """
            idx = ['Year', 'Month', 'Day', 'Hour', 'Minute', 'Second'].index(gra)
            start = tuple(map(int, s.split(':')))[:idx+1]
            end = tuple(map(int, e.split(':')))[:idx+1]
            return list(id for id in self.record if start <= self.record[id][:idx+1] <= end)
    

Log in to reply
 

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