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

边栏推荐
- 【C语言基础】12 字符串
- Judge whether a binary tree is a balanced binary tree
- Please, stop painting star! This has nothing to do with patriotism!
- GaussDB(for MySQL) :Partial Result Cache,通过缓存中间结果对算子进行加速
- How to cancel automatic search and install device drivers for laptops
- 美国国家安全局(NSA)“酸狐狸”漏洞攻击武器平台技术分析报告
- The amazing open source animation library is not only awesome, but also small
- mysql -- explain性能优化
- 存在安全隐患 起亚召回部分K3新能源
- How to maintain the laptop battery
猜你喜欢

如何使用 etcd 实现分布式 /etc 目录

英特尔开源深度学习工具库 OpenVINO,将加大与本土软硬件方合作,持续开放

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"

【splishsplash】关于如何在GUI和json上接收/显示用户参数、MVC模式和GenParam

可迭代对象与迭代器、生成器的区别与联系

为什么你要考虑使用Prisma

Activity的生命周期和启动模式详解

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

How to use F1 to F12 correctly on laptop keyboard

C language input / output stream and file operation
随机推荐
Object. fromEntries()
Leetcode 77 combination -- backtracking method
换掉UUID,NanoID更快更安全!
Rhcsa Road
想做软件测试的女孩子看这里
Shenyu gateway development: enable and run locally
Introduction to software engineering - Chapter 6 - detailed design
Template engine velocity Foundation
Mysql database - Advanced SQL statement (2)
String class
mysql -- explain性能优化
The amazing open source animation library is not only awesome, but also small
中国生物降解塑料市场预测与投资战略报告(2022版)
Judge whether the binary tree is a binary search tree
Rhcsa Road
Rhcsa Road
String的trim()和substring()详解
(27) Open operation, close operation, morphological gradient, top hat, black hat
(1) CNN network structure
SystemVerilog-结构体(二)