Leetcode 535.TinyURL的加密与解密
Time: 20190920
Type: Medium
题目描述
TinyURL是一种URL简化服务, 比如:当你输入一个URL https://leetcode.com/problems/design-tinyurl 时,它将返回一个简化的URL http://tinyurl.com/4e9iAk.
要求:设计一个 TinyURL 的加密 encode 和解密 decode 的方法。你的加密和解密算法如何设计和运作是没有限制的,你只需要保证一个URL可以被加密成一个TinyURL,并且这个TinyURL可以用解密方法恢复成原本的URL。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/encode-and-decode-tinyurl
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路
本题有好几种思路,不过最初看到这个题的时候,我第一感觉是将一个长字符串映射到一个短字符串,同时短字符串还要能反向映射回来。当然这个理解没有错,但忽略了一个要点:TinyURL的前面部分不用修改。
这样我们就可以针对短网址的最后一个字段思考,只需要搞定这个字段和长网址的一一对应即可。
这里列两种方法:
- 计数器法
- 哈希函数法
代码简单易懂,这里不多做解释了~
代码
计数法
class Codec:def __init__(self):self.count = 0self.hashMap = {}def encode(self, longUrl):"""Encodes a URL to a shortened URL.:type longUrl: str:rtype: str"""self.hashMap[str(self.count)] = longUrlres = "http://tinyurl.com/" + str(self.count)self.count += 1return resdef decode(self, shortUrl):"""Decodes a shortened URL to its original URL.:type shortUrl: str:rtype: str"""count = shortUrl.split('/')[-1]return self.hashMap[count]# Your Codec object will be instantiated and called as such:
# codec = Codec()
# codec.decode(codec.encode(url))
哈希函数法
class Codec:def __init__(self):self.hashMap = {}def encode(self, longUrl):"""Encodes a URL to a shortened URL.:type longUrl: str:rtype: str"""self.hashMap[str(hash(longUrl))] = longUrlres = "http://tinyurl.com/" + str(hash(longUrl))return resdef decode(self, shortUrl):"""Decodes a shortened URL to its original URL.:type shortUrl: str:rtype: str"""count = shortUrl.split('/')[-1]return self.hashMap[count]# Your Codec object will be instantiated and called as such:
# codec = Codec()
# codec.decode(codec.encode(url))
2019.10 Update:
第一届PAT算法直播课培训班招募帖,欢迎点击查看详情、
END.
Leetcode 535.TinyURL的加密与解密相关推荐
- Java实现 LeetCode 535 TinyURL 的加密与解密(位运算加密)
535. TinyURL 的加密与解密 TinyURL是一种URL简化服务, 比如:当你输入一个URL https://leetcode.com/problems/design-tinyurl 时,它 ...
- LeetCode 535. TinyURL 的加密与解密(哈希)
文章目录 1. 题目信息 2. 哈希解题 1. 题目信息 TinyURL是一种URL简化服务, 比如:当你输入一个URL https://leetcode.com/problems/design-ti ...
- LeetCode 535. TinyURL 的加密与解密
目录结构 1.题目 2.题解 1.题目 TinyURL是一种URL简化服务, 比如:当你输入一个URL https://leetcode.com/problems/design-tinyurl 时,它 ...
- LeetCode 每日一题——535. TinyURL 的加密与解密
1.题目描述 535. TinyURL 的加密与解密 TinyURL 是一种 URL 简化服务, 比如:当你输入一个 URL https://leetcode.com/problems/design- ...
- 535. TinyURL 的加密与解密 : 设计一个 URL 简化系统
题目描述 这是 LeetCode 上的 「535. TinyURL 的加密与解密」 ,难度为 「中等」. Tag : 「哈希表」.「模拟」 TinyURL 是一种 URL 简化服务, 比如:当你输入一 ...
- leetcode-cpp 535.TinyURL的加密和解密
535.TinyURL的加密和解密 题目: 链接 leetcode solution: 中等难度,总结来说就是看好题目意思就行,有个标志位能够用来区分长长长,mp<string,string&g ...
- 【力扣每日一题】535. TinyURL 的加密与解密
题目描述 535. TinyURL 的加密与解密 难度中等168 TinyURL 是一种 URL 简化服务, 比如:当你输入一个 URL https://leetcode.com/problems/d ...
- 【535. TinyURL 的加密与解密】
来源:力扣(LeetCode) 描述: TinyURL 是一种 URL 简化服务, 比如:当你输入一个 URL https://leetcode.com/problems/design-tinyurl ...
- 535.TinyURL 的加密与解密
TinyURL是一种URL简化服务, 比如:当你输入一个URL https://leetcode.com/problems/design-tinyurl 时,它将返回一个简化的URL http://t ...
最新文章
- spring应用实例
- Centos6.8 Mysql 设置自动备份与定期删除备份文件 自测部署安装
- shell与常用命令
- 如何使用notepad运行python程序
- VS2013崩溃,无法打开项目的解决方案
- Docker系列二~自定义网桥
- 中琛物联‘连接+云+数据’服务助阵
- whitening(白化)
- 飞机选座——附:东航320选坐攻略
- 拒做背锅侠!如何利用网站性能优化驱动产品体验提升
- 电子印章有哪些特点和优势?
- 算法练习-个人所得税
- 【采集项目-(4)业务数据采集】
- htts 及 tomcat ssl配置
- 企业级权限系统架构设计 (v 2.0)
- 数据概览神器—Pandas-profiling
- little alchemy攻略
- mysql_slow_详解MySQL中SlowLog的配置方法(图文)
- dio拦截器 flutter_Flutter开发 Dio拦截器实现token验证过期的功能
- STM32理论 —— 看门狗
热门文章
- html 链接 vf,VFP中超链接实现方法
- 一年级abb式词语并造句_一年级语文ABB式词语专项练习附答案,考考孩子!
- 计算机科技兴趣小组活动总结,信息技术兴趣小组活动总结
- 鸿蒙OS比fuchsia的优势,第一天带你走进华为开发者大会,了解鸿蒙OS
- php安装sphinx扩展,安装php的sphinx扩展模块
- dnspod ddns 下载_简约时尚休闲女装毛衣针织衫春装详情页_psd素材免费下载_ 750*13450像素(编号:24815329)...
- 自编码器模型详解与实现(采用tensorflow2.x实现)
- iOS Swift JSON解析教程
- java treeset_Java TreeSet
- AngularJS自定义指令–隔离范围教程