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 ...
随机推荐
- 西北地区打不开github的解决办法~
泱泱我大西北,github打不开,的确痛苦的. http://ipaddress.com/ip_lookup/ 先查github.com 可能存在打不开的情况~ 随便找一个web在线代理,就可以查到了 ...
- Redis数据结构之robj
本文及后续文章,Redis版本均是v3.2.8 我们知道一个database内的这个映射关系是用一个dict来维护的.dict的key固定用一种数据结构来表达,这这数据结构就是动态字符串sds.而va ...
- 斯特林公式 ——Stirling公式(取N阶乘近似值)
- Python 求点到直线的垂足
Python 求点到直线的垂足 在已知一个点,和一条已知两个点的直线的情况下 运算公式参考链接:https://www.cnblogs.com/mazhenyu/p/3508735.html def ...
- Linux-vi编辑器简单使用(保证存活)
vi编辑器基本模式 命令行模式(command mode) 光标移动.复制粘贴.删除 插入模式(insert mode) 文字输入 底行模式(last line mode) 保存.退出 模式转换 co ...
- python 爬虫与数据可视化--爬虫基础知识
一.python中的模块 模块的安装:pip install 模块名 导入模块与函数:import requests . from pymongo import MongoClient json模块的 ...
- Spark环境搭建(一)-----------HDFS分布式文件系统搭建
下载的压缩文件放在~/software/ 解压安装在~/app/ 一:分布式文件系统搭建HDFS 1,下载Hadoop HDFS环境搭建 使用版本:hadoop-2.6.0-cdh5.7. ...
- Android中的WeakReference 弱引用
WeakReference 弱引用 定义:弱引用,与强引用(我们常见的引用方式)相对:特点是:GC在回收时会忽略掉弱引用对象(忽略掉这种引用关系),即:就算弱引用指向了某个对象,但只要该对象没有被强引 ...
- Git 经常用到的命令
1.克隆master分支之外的分支: 首先克隆项目 1>Git clone git@192.168.0.201:frontend/mn.git 然后转换到克隆下来的文件夹 2>cd 文件名 ...
- jquery easyui datagrid 如何第一次点击列标题时是降序排列
使用 EasyUI的onBeforeLoad事件,在发回到服务器查询之前,修改排序和对应的图标样式. 1.配置回调函数 data-options='onBeforeLoad:fnOnBeforeLoa ...