当前位置:网站首页>LeetCode中等题之TinyURL 的加密与解密
LeetCode中等题之TinyURL 的加密与解密
2022-07-01 16:46:00 【·星辰大海】
题目
TinyURL 是一种 URL 简化服务, 比如:当你输入一个 URL https://leetcode.com/problems/design-tinyurl 时,它将返回一个简化的URL http://tinyurl.com/4e9iAk 。请你设计一个类来加密与解密 TinyURL 。
加密和解密算法如何设计和运作是没有限制的,你只需要保证一个 URL 可以被加密成一个 TinyURL ,并且这个 TinyURL 可以用解密方法恢复成原本的 URL 。
实现 Solution 类:
Solution() 初始化 TinyURL 系统对象。
String encode(String longUrl) 返回 longUrl 对应的 TinyURL 。
String decode(String shortUrl) 返回 shortUrl 原本的 URL 。题目数据保证给定的 shortUrl 是由同一个系统对象加密的。
示例:
输入:url = “https://leetcode.com/problems/design-tinyurl”
输出:“https://leetcode.com/problems/design-tinyurl”
解释:
Solution obj = new Solution();
string tiny = obj.encode(url); // 返回加密后得到的 TinyURL 。
string ans = obj.decode(tiny); // 返回解密后得到的原本的 URL 。
提示:
1 <= url.length <= 10^4
题目数据保证 url 是一个有效的 URL
来源:力扣(LeetCode)
解题思路
在中等题中这是一个比较简单的题目,对于加密解密的方式有很多,一般可以直接在给定的字符串后面添加一个可以直接映射的字符串,可以是随机生成的也可以是固定规律的,在这里为了简便直接使用简单的递增的下标即可。
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))

边栏推荐
- Shenyu gateway development: enable and run locally
- Redis 分布式鎖
- Introduction to software engineering - Chapter 6 - detailed design
- Redis6.0 new features
- 走进微信小程序
- GaussDB(for MySQL) :Partial Result Cache,通过缓存中间结果对算子进行加速
- 存在安全隐患 起亚召回部分K3新能源
- Graduation season | Huawei experts teach the interview secret: how to get a high paying offer from a large factory?
- Dataframe gets the number of words in the string
- Research and investment strategy report of neutral protease industry in China (2022 Edition)
猜你喜欢

ShenYu 网关开发:在本地启用运行

美国国家安全局(NSA)“酸狐狸”漏洞攻击武器平台技术分析报告

Gold, silver and four want to change jobs, so we should seize the time to make up
![[mathematical modeling] [matlab] implementation of two-dimensional rectangular packing code](/img/de/1f572c62a0d034da9a8acb5c2f9602.jpg)
[mathematical modeling] [matlab] implementation of two-dimensional rectangular packing code

Bugku's file contains

Iommu/smmuv3 code analysis (10) page table operation
![[flask introduction series] cookies and session](/img/2e/d50e0a032c4ec48935cb5df206a29b.png)
[flask introduction series] cookies and session

sql刷题584. 寻找用户推荐人

Stegano in the world of attack and defense

免费抽奖 | 《阿巴豆》探索未来系列盲盒数字版权作品全网首发!
随机推荐
Tutorial on the principle and application of database system (001) -- MySQL installation and configuration: installation of MySQL software (Windows Environment)
PR basic clip operation / video export operation
China benzene hydrogenation Market Research and investment forecast report (2022 Edition)
mysql -- explain性能优化
Rhcsa Road
阿里云、追一科技抢滩对话式AI
Kali install Nessus
SQL question brushing 1050 Actors and directors who have worked together at least three times
Judge whether a binary tree is a balanced binary tree
中国酶制剂市场预测与投资战略研究报告(2022版)
存在安全隐患 起亚召回部分K3新能源
SQL question brushing 586 Customers with the most orders
Iommu/smmuv3 code analysis (10) page table operation
中国生物降解塑料市场预测与投资战略报告(2022版)
拼接字符串,得到字典序最小的结果
SystemVerilog structure (II)
Judge whether the binary tree is a binary search tree
FRP intranet penetration, reverse proxy
Installation and use of sqoop
How wild are hackers' ways of making money? CTF reverse entry Guide