defencode(self, longUrl: str) -> str: """Encodes a URL to a shortened URL. """ k=1117 m=10**9+7 h,base=0,1 for c in longUrl: h+=ord(c)*base%m base=base*k%m while h in self.dic and self.dic[h]!=longUrl: h+=1 self.dic[h]=longUrl returnf'http://tinyurl.com/{h}'
defdecode(self, shortUrl: str) -> str: """Decodes a shortened URL to its original URL. """ return self.dic[int(shortUrl.split('/')[-1])]
# 62进制表示的字符串转换为整型数值 deftoInt(self,num): r,base = 0,1 for c in num: r+=self.map[c] * base base*=self.base return r
defencode(self, longUrl: str) -> str: """Encodes a URL to a shortened URL. """ k = 1117 m = 10 ** 9 + 7 h,base = 0,1 for c in longUrl: h+=ord(c) * base % m base = base * k % m while h in self.dic and self.dic[h] != longUrl: h+=1 self.dic[h] = longUrl returnf'http://tinyurl.com/{self.toStr(h)}'
defdecode(self, shortUrl: str) -> str: """Decodes a shortened URL to its original URL. """ return self.dic[self.toInt(shortUrl.split('/')[-1])]