LeetCode_Encode and Decode TinyURL

오범준·2020년 12월 25일
0
class Codec:
    
    def __init__(self):
        self.lookup = []

    def encode(self, longUrl):
        """Encodes a URL to a shortened URL.
        
        :type longUrl: str
        :rtype: str
        """
        N = len(self.lookup)
        print("N in encode", N)
        self.lookup.append(longUrl)
        return 'http://tinyurl.com/' + str(N)
        

    def decode(self, shortUrl):
        """Decodes a shortened URL to its original URL.
        
        :type shortUrl: str
        :rtype: str
        """
        r = int(shortUrl.split('/')[-1])
        return self.lookup[r]
        

# Your Codec object will be instantiated and called as such:
# codec = Codec()
# codec.decode(codec.encode(url))

원리 : 해쉬 테이블 이용하기

즉, 0 이라는 key값을 두고
그 key 값에 대한 value로
원본 longUrl을 출력한다

profile
Dream of being "물빵개" ( Go abroad for Dance and Programming)

0개의 댓글

관련 채용 정보