当前位置:网站首页>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))
边栏推荐
- Gold, silver and four want to change jobs, so we should seize the time to make up
- How to cancel automatic search and install device drivers for laptops
- 中国PBAT树脂市场预测及战略研究报告(2022版)
- Exclusive news: Alibaba cloud quietly launched RPA cloud computer and has opened cooperation with many RPA manufacturers
- National Security Agency (NSA) "sour Fox" vulnerability attack weapon platform technical analysis report
- How wild are hackers' ways of making money? CTF reverse entry Guide
- 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
- Redis 分布式锁
- Pytest learning notes (13) -allure of allure Description () and @allure title()
- 为什么你要考虑使用Prisma
猜你喜欢
Introduction to software engineering - Chapter 6 - detailed design
Redis6.0 新功能
软件工程导论——第六章——详细设计
There is a new breakthrough in quantum field: the duration of quantum state can exceed 5 seconds
Girls who want to do software testing look here
智能运维实战:银行业务流程及单笔交易追踪
C语言输入/输出流和文件操作
Basic usage of Frida
PR basic clip operation / video export operation
How to maintain the laptop battery
随机推荐
Iommu/smmuv3 code analysis (10) page table operation
SQL question brushing 584 Looking for user references
Introduction to software engineering - Chapter 6 - detailed design
【Try to Hack】vulnhub DC4
[wrung Ba wrung Ba is 20] [essay] why should I learn this in college?
What is the effect of choosing game shield safely in the game industry?
Jojogan practice
String的trim()和substring()详解
中国超高分子量聚乙烯产业调研与投资前景报告(2022版)
How to restore the system of Sony laptop
美国国家安全局(NSA)“酸狐狸”漏洞攻击武器平台技术分析报告
Judge whether a binary tree is a balanced binary tree
判断链表是否是回文链表
中国茂金属聚乙烯(mPE)行业研究报告(2022版)
Research and investment strategy report of neutral protease industry in China (2022 Edition)
【牛客网刷题系列 之 Verilog快速入门】~ 优先编码器电路①
Installation and use of sqoop
In aks, use secret in CSI driver mount key vault
(28) Shape matching based on contour features
Object. fromEntries()