题目

Note: This is a companion problem to the System Design problem: Design TinyURL.

TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problems/design-tinyurl and it returns a short URL such as http://tinyurl.com/4e9iAk.

Design the encode and decode methods for the TinyURL service. There is no restriction on how your encode/decode algorithm should work. You just need to ensure that a URL can be encoded to a tiny URL and the tiny URL can be decoded to the original URL.

分析

题意:设计一个简化URL的编码算法,比如
https://leetcode.com/problems/design-tinyurl编码为http://tinyurl.com/4e9iAk
当然http://tinyurl.com/4e9iAk解码的结果也为https://leetcode.com/problems/design-tinyurl

这个是开放性的题目,自行设计即可。
个人想法(以https://leetcode.com/problems/design-tinyurl为例):
https://leetcode.com/保持原样。我们假设所有的URL都要进行编码,那么主域名就没有编码的必要了。

https://leetcode.com/problems/design-tinyurl为例,个人觉得难点有两点:
1.xxx/design-tinyurl和xxx/design-tinyuri也应当被识别,因此url的识别粒度在单个符号的层面。
2.URL如何缩短–>26个字母+n个符号如何转为更少的符号表示–>如何保证26个字母+n个符号的转换过程可逆。

比如说1,2,3将其转为一个数的方法为1+2+3=6,但是6并不能唯一的转为1,2,3,因此这种方法不行。
难点就在于如何缩短之后还能保证可逆?

有哪些运算是多元运算得出单个结果,并且该结果也能逆运算为多个元?
百度了解了下,应该跟要使用压缩算法。
了解了下zip算法,也有个想法,但是写了半天写不下去了。

去看看别人的做法。

看完别人的做法。。。看样子是我想多了。

算法

将原始的uri对应任意一个唯一的字符,存放到map中,如{原始uri:唯一字符},并备份一份{唯一字符:原始uri}。
编码的时候就是`原始uri映射到唯一字符`,解码的时候用`唯一字符到原始uri的map`即可。

解答

public class Codec {//{识别字符:原始uri}Map<String, String> index = new HashMap<String, String>();//{原始uri,识别字符}Map<String, String> revIndex = new HashMap<String, String>();static String BASE_HOST = "http://tinyurl.com/";// Encodes a URL to a shortened URL.public String encode(String longUrl) {if (revIndex.containsKey(longUrl)) return BASE_HOST + revIndex.get(longUrl);String charSet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";String key = null;do {StringBuilder sb = new StringBuilder();for (int i = 0; i < 6; i++) {int r = (int) (Math.random() * charSet.length());sb.append(charSet.charAt(r));}key = sb.toString();} while (index.containsKey(key));index.put(key, longUrl);revIndex.put(longUrl, key);return BASE_HOST + key;}// Decodes a shortened URL to its original URL.public String decode(String shortUrl) {return index.get(shortUrl.replace(BASE_HOST, ""));}
}

简化版

    Map<Integer, String> map = new HashMap();String host = "http://tinyurl.com/";public String encode(String longUrl) {int key = longUrl.hashCode();map.put(key, longUrl);return host+key;}public String decode(String shortUrl) {int key = Integer.parseInt(shortUrl.replace(host,""));return map.get(key);}

49 Encode and Decode TinyURL相关推荐

  1. 2019年2月26日 Unique Email Addresses、To Lower Case、Encode and Decode TinyURL

    今天开始加快速度,趁着还有空多刷几题,语言换成python提高速度了. 1. Unique Email Addresses 弱题,注意@符号前后的处理方式不同 class Solution(objec ...

  2. LeetCode 535. Encode and Decode TinyURL

    题目: Note: This is a companion problem to the System Design problem: Design TinyURL. TinyURL is a URL ...

  3. 【leetcode】535. Encode and Decode TinyURL

    原题 TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problems/d ...

  4. LeetCode Encode and Decode TinyURL

    1232131 转载于:https://www.cnblogs.com/ZHONGZHENHUA/p/10927802.html

  5. python3 的encode 和 decode

    str ----> bytes: encode 编码 bytes----> str: decode 解码 str.encode() bytes.decode() >>> ...

  6. decode函数python在哪里_Python基础知识——encode和decode函数

    以前我们介绍过,Python2.x中默认的编码的基础类型是unicode编码的类型,在Python3.x才转化为基于unicode的字符串. 那么我们在Python2.x的学习中就会遇到各种各样的编码 ...

  7. pythonunicode和str_python的str,unicode对象的encode和decode方法

    python的str,unicode对象的encode和decode方法 python中的str对象其实就是"8-bit string" ,字节字符串,本质上类似java中的byt ...

  8. python中的encode()和decode()函数

    对于很多人来说,python的中字符转码是一件很头疼的事情,本来期望结果输出的是中文,结果来一段像这样\xe4\xbd\xa0\xe5\xa5\xbd像是乱码的字符串. 由于学python没多久,昨天 ...

  9. Python zipfile 文件名称编码 file_name.encode(‘cp437‘).decode(‘gbk‘)

    主要是处理压缩包里的中文,不出现乱码 with zipfile.ZipFile('love.zip', 'r') as zipobj: #读取压缩包for file_name in zipobj.na ...

最新文章

  1. 信科c语言实验程序修改题_豆瓣评分 9.3,史上最好的 C 语言著作,竟然翻车了.........
  2. tomcat升级后报错: Invalid character found in the request target.
  3. Android 多线程及线程通信
  4. 经典算法——KMP模式匹配
  5. docker安装运行rancher脚本
  6. codesmith使用的一个小问题
  7. honeywell新风系统控制面板说明_新风系统——不开窗也能清新呼吸
  8. git和gitlab安装
  9. 融资12亿后倒闭 这家机器人公司宣布全体裁员
  10. java 构建_Java入门环境构建
  11. Eclipse环境下配置Tomcat
  12. 图解TCPIP---第二章
  13. 世嘉MD游戏开发【十三】:音乐和音效
  14. 模拟电子技术学习-三极管的伏安特性
  15. 北大AI讲座公开课-精华
  16. Technorati 正在走向衰落
  17. android本地视频 投屏,小米投屏神器
  18. Jmeter遇到打不开的问题
  19. 求告知pycharm的这条边界线怎么设置
  20. OpenCV-DoG

热门文章

  1. BZOJ1567 [JSOI2008]Blue Mary的战役地图
  2. 用空格分割的多关键字搜索
  3. 快讯:农作物精准育种技术的重大进展
  4. 在layui数据表格中修改数据
  5. 如何擦除FLASH解决方法
  6. 【预测模型】基于遗传算法优化GRNN实现数据分类matlab源码
  7. 针对科学数据处理的统计学习教程(scikit-learn教程2)
  8. 3d建模真的很累吗?前景怎么样?
  9. 最近 twitter 类网站大爆发啊
  10. pgadmin转mysql_pgAdmin 使用总结及postgreSQL常用操作