当前位置:网站首页>Encryption and decryption of tinyurl in leetcode
Encryption and decryption of tinyurl in leetcode
2022-07-01 17:02:00 【·Starry Sea】
subject
TinyURL It's a kind of URL Simplify services , such as : When you enter a URL https://leetcode.com/problems/design-tinyurl when , It will return a simplified URL http://tinyurl.com/4e9iAk . Please design a class to encrypt and decrypt TinyURL .
There is no limit to how encryption and decryption algorithms are designed and operated , You just need to guarantee one URL Can be encrypted into a TinyURL , And this TinyURL It can be restored to the original by decryption URL .
Realization Solution class :
Solution() initialization TinyURL System object .
String encode(String longUrl) return longUrl Corresponding TinyURL .
String decode(String shortUrl) return shortUrl The original URL . The subject data guarantees a given shortUrl Is encrypted by the same system object .
Example :
Input :url = “https://leetcode.com/problems/design-tinyurl”
Output :“https://leetcode.com/problems/design-tinyurl”
explain :
Solution obj = new Solution();
string tiny = obj.encode(url); // Returns the result of encryption TinyURL .
string ans = obj.decode(tiny); // Returns the original after decryption URL .
Tips :
1 <= url.length <= 10^4
Topic data assurance url Is an effective URL
source : Power button (LeetCode)
Their thinking
In the medium question, this is a relatively simple question , There are many ways to encrypt and decrypt , Generally, you can add a string that can be mapped directly after a given string , It can be randomly generated or fixed , Here, for simplicity, you can directly use a simple increasing subscript .
class Codec:
def __init__(self):
self.n=0
self.webset=dict()
def encode(self, longUrl: str) -> str:
"""Encodes a URL to a shortened URL. """
self.webset[self.n]=longUrl
return 'http://tinyurl.com/'+str(self.n)
def decode(self, shortUrl: str) -> str:
"""Decodes a shortened URL to its original URL. """
return self.webset[int(shortUrl.split('/')[-1])]
# Your Codec object will be instantiated and called as such:
# codec = Codec()
# codec.decode(codec.encode(url))

边栏推荐
猜你喜欢

Are you still using charged document management tools? I have a better choice! Completely free

How to restore the system with one click on Lenovo laptop

Redis Distributed Lock

Template engine velocity Foundation

【C补充】【字符串】按日期排序显示一个月的日程

What is the effect of choosing game shield safely in the game industry?

(28) Shape matching based on contour features

The amazing open source animation library is not only awesome, but also small

英特尔开源深度学习工具库 OpenVINO,将加大与本土软硬件方合作,持续开放

In aks, use secret in CSI driver mount key vault
随机推荐
How to use F1 to F12 correctly on laptop keyboard
6月刊 | AntDB数据库参与编写《数据库发展研究报告》 亮相信创产业榜单
如何写出好代码 — 防御式编程指南
In aks, use secret in CSI driver mount key vault
Today, at 14:00, 15 ICLR speakers from Hong Kong University, Beihang, Yale, Tsinghua University, Canada, etc. continue!
Flux d'entrées / sorties et opérations de fichiers en langage C
Leetcode 77 combination -- backtracking method
[mathematical modeling] [matlab] implementation of two-dimensional rectangular packing code
为什么你要考虑使用Prisma
Transition technology from IPv4 to IPv6
[flask introduction series] cookies and session
判断二叉树是否为二叉搜索树
ACL 2022 | 分解的元学习小样本命名实体识别
Mysql database - Advanced SQL statement (2)
中国一次性卫生用品生产设备行业深度调研报告(2022版)
China nylon 11 industry research and future forecast report (2022 Edition)
sql刷题586. 订单最多的客户
Sword finger offer II 015 All modifiers in the string
Borui data integrated intelligent observable platform was selected into the "Yunyuan production catalogue" of China Academy of communications in 2022
sql刷题627. 变更性别