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

边栏推荐
- 英特尔开源深度学习工具库 OpenVINO,将加大与本土软硬件方合作,持续开放
- Gold, silver and four want to change jobs, so we should seize the time to make up
- 【C补充】【字符串】按日期排序显示一个月的日程
- How to restore the system with one click on Lenovo laptop
- sql刷题1050. 合作过至少三次的演员和导演
- Kali install Nessus
- 软件工程导论——第六章——详细设计
- Internet News: "20220222" get together to get licenses; Many products of Jimi have been affirmed by consumers; Starbucks was fined for using expired ingredients in two stores
- 美国国家安全局(NSA)“酸狐狸”漏洞攻击武器平台技术分析报告
- ACM MM 2022视频理解挑战赛视频分类赛道冠军AutoX团队技术分享
猜你喜欢

Alibaba cloud, Zhuoyi technology beach grabbing dialogue AI

Flux d'entrées / sorties et opérations de fichiers en langage C

Internet News: "20220222" get together to get licenses; Many products of Jimi have been affirmed by consumers; Starbucks was fined for using expired ingredients in two stores

SQL question brushing 586 Customers with the most orders

Dataframe gets the number of words in the string

SQL question brushing 584 Looking for user references

整形数组合并【JS】

Soft test network engineer full truth simulation question (including answer and analysis)

Redis6.0 new features

How to use etcd to realize distributed /etc directory
随机推荐
ShenYu 网关开发:在本地启用运行
整形数组合并【JS】
判断二叉树是否为二叉搜索树
redis -- 数据类型及操作
Report on Market Research and investment prospects of ammonium dihydrogen phosphate industry in China (2022 Edition)
String class
C language input / output stream and file operation
China benzene hydrogenation Market Research and investment forecast report (2022 Edition)
[flask introduction series] cookies and session
Template engine velocity Foundation
6月刊 | AntDB数据库参与编写《数据库发展研究报告》 亮相信创产业榜单
荣威 RX5 的「多一点」产品策略
Judge whether the binary tree is a binary search tree
China sorbitol Market Forecast and investment strategy report (2022 Edition)
Yyds dry inventory MySQL RC transaction isolation level implementation
Transition technology from IPv4 to IPv6
Computed property “xxx“ was assigned to but it has no setter.
How to cancel automatic search and install device drivers for laptops
sql刷题584. 寻找用户推荐人
美国国家安全局(NSA)“酸狐狸”漏洞攻击武器平台技术分析报告