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的加密与解密相关推荐

  1. Java实现 LeetCode 535 TinyURL 的加密与解密(位运算加密)

    535. TinyURL 的加密与解密 TinyURL是一种URL简化服务, 比如:当你输入一个URL https://leetcode.com/problems/design-tinyurl 时,它 ...

  2. LeetCode 535. TinyURL 的加密与解密(哈希)

    文章目录 1. 题目信息 2. 哈希解题 1. 题目信息 TinyURL是一种URL简化服务, 比如:当你输入一个URL https://leetcode.com/problems/design-ti ...

  3. LeetCode 535. TinyURL 的加密与解密

    目录结构 1.题目 2.题解 1.题目 TinyURL是一种URL简化服务, 比如:当你输入一个URL https://leetcode.com/problems/design-tinyurl 时,它 ...

  4. LeetCode 每日一题——535. TinyURL 的加密与解密

    1.题目描述 535. TinyURL 的加密与解密 TinyURL 是一种 URL 简化服务, 比如:当你输入一个 URL https://leetcode.com/problems/design- ...

  5. 535. TinyURL 的加密与解密 : 设计一个 URL 简化系统

    题目描述 这是 LeetCode 上的 「535. TinyURL 的加密与解密」 ,难度为 「中等」. Tag : 「哈希表」.「模拟」 TinyURL 是一种 URL 简化服务, 比如:当你输入一 ...

  6. leetcode-cpp 535.TinyURL的加密和解密

    535.TinyURL的加密和解密 题目: 链接 leetcode solution: 中等难度,总结来说就是看好题目意思就行,有个标志位能够用来区分长长长,mp<string,string&g ...

  7. 【力扣每日一题】535. TinyURL 的加密与解密

    题目描述 535. TinyURL 的加密与解密 难度中等168 TinyURL 是一种 URL 简化服务, 比如:当你输入一个 URL https://leetcode.com/problems/d ...

  8. 【535. TinyURL 的加密与解密】

    来源:力扣(LeetCode) 描述: TinyURL 是一种 URL 简化服务, 比如:当你输入一个 URL https://leetcode.com/problems/design-tinyurl ...

  9. 535.TinyURL 的加密与解密

    TinyURL是一种URL简化服务, 比如:当你输入一个URL https://leetcode.com/problems/design-tinyurl 时,它将返回一个简化的URL http://t ...

最新文章

  1. spring应用实例
  2. Centos6.8 Mysql 设置自动备份与定期删除备份文件 自测部署安装
  3. shell与常用命令
  4. 如何使用notepad运行python程序
  5. VS2013崩溃,无法打开项目的解决方案
  6. Docker系列二~自定义网桥
  7. 中琛物联‘连接+云+数据’服务助阵
  8. whitening(白化)
  9. 飞机选座——附:东航320选坐攻略
  10. 拒做背锅侠!如何利用网站性能优化驱动产品体验提升
  11. 电子印章有哪些特点和优势?
  12. 算法练习-个人所得税
  13. 【采集项目-(4)业务数据采集】
  14. htts 及 tomcat ssl配置
  15. 企业级权限系统架构设计 (v 2.0)
  16. 数据概览神器—Pandas-profiling
  17. little alchemy攻略
  18. mysql_slow_详解MySQL中SlowLog的配置方法(图文)
  19. dio拦截器 flutter_Flutter开发 Dio拦截器实现token验证过期的功能
  20. STM32理论 —— 看门狗

热门文章

  1. html 链接 vf,VFP中超链接实现方法
  2. 一年级abb式词语并造句_一年级语文ABB式词语专项练习附答案,考考孩子!
  3. 计算机科技兴趣小组活动总结,信息技术兴趣小组活动总结
  4. 鸿蒙OS比fuchsia的优势,第一天带你走进华为开发者大会,了解鸿蒙OS
  5. php安装sphinx扩展,安装php的sphinx扩展模块
  6. dnspod ddns 下载_简约时尚休闲女装毛衣针织衫春装详情页_psd素材免费下载_ 750*13450像素(编号:24815329)...
  7. 自编码器模型详解与实现(采用tensorflow2.x实现)
  8. iOS Swift JSON解析教程
  9. java treeset_Java TreeSet
  10. AngularJS自定义指令–隔离范围教程