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

边栏推荐
- Please, stop painting star! This has nothing to do with patriotism!
- Redis6.0 新功能
- 【flask入门系列】Cookie与Session
- Sword finger offer II 015 All modifiers in the string
- Leetcode 77 combination -- backtracking method
- (28) Shape matching based on contour features
- libcurl下载文件的代码示例
- SQL注入漏洞(Mysql与MSSQL特性)
- 中国乙腈市场预测与战略咨询研究报告(2022版)
- [flask introduction series] cookies and session
猜你喜欢

(1) CNN network structure

游戏行业安全选择游戏盾,效果怎么样?

Installation and use of sqoop

为什么你要考虑使用Prisma

字节跳动数据平台技术揭秘:基于 ClickHouse 的复杂查询实现与优化

模板引擎Velocity 基礎

Cookies and session keeping technology

巴比特 | 元宇宙每日必读:奈雪币、元宇宙乐园、虚拟股票游戏...奈雪的茶这波“操作拉满”的营销活动你看懂了吗?...

模板引擎Velocity 基础

Babbitt | yuan universe daily must read: Naixue coin, Yuan universe paradise, virtual stock game Do you understand Naixue's tea's marketing campaign of "operation pull full"
随机推荐
libcurl下载文件的代码示例
换掉UUID,NanoID更快更安全!
软件工程导论——第六章——详细设计
Gold, silver and four want to change jobs, so we should seize the time to make up
sql刷题584. 寻找用户推荐人
What is the effect of choosing game shield safely in the game industry?
Template engine velocity Foundation
中国锦纶长丝缝纫线发展预测与投资方向研究报告(2022版)
Today, at 14:00, 15 ICLR speakers from Hong Kong University, Beihang, Yale, Tsinghua University, Canada, etc. continue!
《中国智慧环保产业发展监测与投资前景研究报告(2022版)》
SystemVerilog structure (II)
How to cancel automatic search and install device drivers for laptops
为什么你要考虑使用Prisma
How to use F1 to F12 correctly on laptop keyboard
sql刷题1050. 合作过至少三次的演员和导演
走进微信小程序
Please, stop painting star! This has nothing to do with patriotism!
SQL question brushing 584 Looking for user references
Rhcsa Road
(12) About time-consuming printing