Leetcode: Encode and Decode TinyURL
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. Subscribe to see which companies asked this question.
1. 根据系统设计的Estimation of the amount of data we need to store for the next couple of years, 我们应需要6位Base62的char来encode
2. assume 避免地址爆炸,相同的longUrl得到相同的shortUrl, 这需要一个额外的hashMap longToShort
3. 这里因为我们想保证都是6位的shortURL,所以采用random generate的方法;其他方法还可以是编号等等
public class Codec {
HashMap<String, String> hashToUrl = new HashMap<String, String>();
HashMap<String, String> urlToHash = new HashMap<String, String>();
String tinyUrlBase = "http://tinyurl.com/";
String characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
Random random = new Random();
// Encodes a URL to a shortened URL.
public String encode(String longUrl) {
if (urlToHash.containsKey(longUrl))
return tinyUrlBase + urlToHash.get(longUrl);
StringBuilder hash = new StringBuilder();
do {
for (int i=0; i<6; i++) {
hash.append(characters.charAt(random.nextInt(characters.length())));
}
} while (hashToUrl.containsKey(hash.toString()));
hashToUrl.put(hash.toString(), longUrl);
urlToHash.put(longUrl, hash.toString());
return tinyUrlBase + hash.toString();
}
// Decodes a shortened URL to its original URL.
public String decode(String shortUrl) {
return hashToUrl.get(shortUrl.substring(tinyUrlBase.length()));
}
}
// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.decode(codec.encode(url));
Leetcode: Encode and Decode TinyURL的更多相关文章
- [LeetCode] Encode and Decode TinyURL 编码和解码精简URL地址
Note: This is a companion problem to the System Design problem: Design TinyURL. TinyURL is a URL sho ...
- 535. Encode and Decode TinyURL - LeetCode
Question 535. Encode and Decode TinyURL Solution 题目大意:实现长链接加密成短链接,短链接解密成长链接 思路:加密成短链接+key,将长链接按key保存 ...
- LC 535. Encode and Decode TinyURL
Note: This is a companion problem to the System Design problem: Design TinyURL. TinyURL is a URL sho ...
- 【Leetcode】535. Encode and Decode TinyURL
Question: TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/pro ...
- [LeetCode] 535. Encode and Decode TinyURL 编码和解码短网址
Note: This is a companion problem to the System Design problem: Design TinyURL. TinyURL is a URL sho ...
- 【LeetCode】535. Encode and Decode TinyURL 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 方法一:数组 方法二:字典 日期 题目地址:https://l ...
- Encode and Decode TinyURL
TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problems/desi ...
- 535. Encode and Decode TinyURL 长短URL
[抄题]: TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problem ...
- 535. Encode and Decode TinyURL(rand and srand)
Note: This is a companion problem to the System Design problem: Design TinyURL. TinyURL is a URL sho ...
随机推荐
- javascript 通过模块模式实现代码访问控制
<script type="text/javascript"> // 在匿名函数中使用var关键字 (function(){ var privateField = 42 ...
- PostgreSQL自学笔记:6 PostgreSQL函数
6 PostgreSQL函数 6.2 数学函数 abs(x) 绝对值 pi() 圆周率π select abs(-3),pi(); cookie: MySQL中的pi()默认值3.141593, Po ...
- Android源代码编译过程及指令
编译Android源代码分为两种情况: 1. 完整编译源码: ./mk_aliphone.sh --> 完整编译脚本 --> 6735 输入对应的编号 --> userdebug ...
- C# - 什么是事件绑定?
今天在学习C#时碰到了一个新词:“绑定事件”,不知道是什么东西? 请各位C#前辈指点!!!
- Luogu 45887 全村最好的嘤嘤刀(线段树 树状数组)
https://www.luogu.org/problemnew/show/T45887 题目背景 重阳节到了,我们最好的八重樱拥有全村最好的嘤嘤刀…… 题目描述 在绯玉丸力量的影响下,八重村成了一条 ...
- windows上react-native run-android时Exception in thread "main" java.lang.IllegalArgumentException: MALFORMED报错
报错如图 解决 在C:\Users\{用户名}\.gradle\wrapper\dists路径下,删除所有文件夹,重新run-android ps:网上搜了说是说是java解压缩编码格式问题什么的,感 ...
- __x__(11)0906第三天__图片标签
图片标签 <img src="images/1.gif" alt="冰河世纪的大松鼠" width="80%" /> Hell ...
- (95)Wangdao.com_第二十八天_进度事件
进度事件 进度事件 用来描述资源加载的进度, 主要由 AJAX 请求.<img>.<audio>.<video>.<style>.<link> ...
- HDFS基础配置
HADOOP-3.1.0-----HDFS基础配置 执行步骤:(1)配置集群(2)启动.测试集群增.删.查(3)执行wordcount案例 一.配置集群 1.在 hadoop-env.sh配置文件添加 ...
- hdfs OutOfMemoryError
大量推送本地文件到hdfs如下 hadoop fs -put ${local_path} ${hdfs_path}报错. Exception in thread "main" ja ...