当前位置:网站首页>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))
边栏推荐
- 在MeterSphere接口测试中如何使用JMeter函数和MockJS函数
- 【C语言补充】判断明天是哪一天(明天的日期)
- Determine whether the linked list is a palindrome linked list
- Transition technology from IPv4 to IPv6
- Chinese diosgenin market forecast and investment strategy report (2022 Edition)
- Leetcode records - sort -215, 347, 451, 75
- 如何写出好代码 — 防御式编程指南
- Detailed explanation of activity life cycle and startup mode
- Girls who want to do software testing look here
- How to cancel automatic search and install device drivers for laptops
猜你喜欢
[mathematical modeling] [matlab] implementation of two-dimensional rectangular packing code
巴比特 | 元宇宙每日必读:奈雪币、元宇宙乐园、虚拟股票游戏...奈雪的茶这波“操作拉满”的营销活动你看懂了吗?...
走进微信小程序
Tutorial on the principle and application of database system (001) -- MySQL installation and configuration: installation of MySQL software (Windows Environment)
Redis6.0 new features
String类
(1) CNN network structure
模板引擎Velocity 基礎
GameFramework食用指南
SQL question brushing 627 Change gender
随机推荐
字节跳动数据平台技术揭秘:基于 ClickHouse 的复杂查询实现与优化
MySQL learning summary
游戏行业安全选择游戏盾,效果怎么样?
6月刊 | AntDB数据库参与编写《数据库发展研究报告》 亮相信创产业榜单
[live broadcast appointment] database obcp certification comprehensive upgrade open class
P2893 [usaco08feb] making the grade g (DP & priority queue)
What is the effect of choosing game shield safely in the game industry?
Judge whether the binary tree is a binary search tree
拼接字符串,得到字典序最小的结果
AI高考志愿填报:大厂神仙打架,考生付费围观
[jetsonnano] [tutorial] [introductory series] [III] build tensorflow environment
[wrung Ba wrung Ba is 20] [essay] why should I learn this in college?
Graduation season | Huawei experts teach the interview secret: how to get a high paying offer from a large factory?
How to solve the keyboard key failure of notebook computer
如何写出好代码 — 防御式编程指南
剑指 Offer II 105. 岛屿的最大面积
判断二叉树是否为二叉搜索树
Building blocks for domestic databases, stonedb integrated real-time HTAP database is officially open source!
为什么你要考虑使用Prisma
[flask introduction series] cookies and session