当前位置:网站首页>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))

边栏推荐
- 巴比特 | 元宇宙每日必读:奈雪币、元宇宙乐园、虚拟股票游戏...奈雪的茶这波“操作拉满”的营销活动你看懂了吗?...
- 中国一次性卫生用品生产设备行业深度调研报告(2022版)
- 在MeterSphere接口测试中如何使用JMeter函数和MockJS函数
- How to cancel automatic search and install device drivers for laptops
- Research and investment strategy report of hydroxypropyl beta cyclodextrin industry in China (2022 Edition)
- 《中国智慧环保产业发展监测与投资前景研究报告(2022版)》
- SQL question brushing 586 Customers with the most orders
- Machine learning 11 clustering, outlier discrimination
- Free lottery | explore the future series of blind box digital copyright works of "abadou" will be launched on the whole network!
- SystemVerilog structure (II)
猜你喜欢

Iommu/smmuv3 code analysis (10) page table operation

软件工程导论——第六章——详细设计

Borui data integrated intelligent observable platform was selected into the "Yunyuan production catalogue" of China Academy of communications in 2022

sql刷题627. 变更性别

Detailed explanation of activity life cycle and startup mode

Today, at 14:00, 15 ICLR speakers from Hong Kong University, Beihang, Yale, Tsinghua University, Canada, etc. continue!

In aks, use secret in CSI driver mount key vault

How to use F1 to F12 correctly on laptop keyboard

Redis distributed lock

美国国家安全局(NSA)“酸狐狸”漏洞攻击武器平台技术分析报告
随机推荐
拼接字符串,得到字典序最小的结果
字节跳动数据平台技术揭秘:基于 ClickHouse 的复杂查询实现与优化
【C补充】【字符串】按日期排序显示一个月的日程
Judge whether a binary tree is a balanced binary tree
重磅披露!上百个重要信息系统被入侵,主机成为重点攻击目标
求求你们,别再刷 Star 了!这跟“爱国”没关系!
Cookies and session keeping technology
整形数组合并【JS】
游戏行业安全选择游戏盾,效果怎么样?
想做软件测试的女孩子看这里
(28) Shape matching based on contour features
Redis Distributed Lock
【C語言補充】判斷明天是哪一天(明天的日期)
[live broadcast appointment] database obcp certification comprehensive upgrade open class
(1) CNN network structure
String的trim()和substring()详解
How to restore the system with one click on Lenovo laptop
Introduction to software engineering - Chapter 6 - detailed design
GaussDB(for MySQL) :Partial Result Cache,通过缓存中间结果对算子进行加速
How to solve the keyboard key failure of notebook computer