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 ...
随机推荐
- 关于input的检验问题
写了很多小应用 但是 对于input有很多 相同的需求 在这里做一个总结 将用的多的校验方法 封装为方法 使用 1.只能输入正整数的校验 输入的时候同时校验 将字符类型的全部替换为空 <inpu ...
- Python学习(三十七)—— 模板语言之自定义filter和中间件
一.模板语言之自定义filter 自定义filter文件存放位置 模板中自定义函数 - 在已注册的app中创建一个名字叫 templatetags 文件夹 - 任意创建一个py文件 - 创建名字交 r ...
- elasticsearch5.5.3 源码学习 idea下源码编译
1.学习elasticsearch 源码,通过搜索“elasticsearch源码”,进行相关搜索. 2.因源码gradle编译,选择gradle-3.5可以编译通过,对应elasticsearc ...
- 软件工程团队:Spring计划会议及详细计划表
极限挑战! 小组Spring计划表: 11.15 进行软件需求分析,了解调查社会背景,确定要编写的软件,分配各小组成员的任务.确定小组会议每天召开地点时间. 3h 11.16 将任务进一步精确分配, ...
- org.hibernate.exception.SQLGrammarException: could not extract ResultSet &&&&&Incorrect syntax near '@P0'.
这个故障的原因比较多: 1.如数据库中的字段和类中的字段类型不一致: 2.数据库dialect不够具体 myeclispe自动生成的是 org.hibernate.dialect.SQLServer ...
- Qt写websocketpp服务端
1.下载websocketpp,地址为https://github.com/zaphoyd/websocketpp,版本为0.7. 2.下载boost,地址为https://www.boost.org ...
- 版本管理_svn_检出_更新_提交_本机 MAC 地址_冲突
版本管理 git svn [subversion] 实现集中式的版本控制工具软件 来源于最初的工程图纸. 需求: 备份还原 协同修改 权限控制 弊端:集中式(管理方便,但是如果中心服务器瘫痪,整个开发 ...
- Node.js_简介及其 npm 包管理器基本使用_npm_cnpm_yarn_cyarn
Node.js 既是语言也是平台,跳过了 Apache.Nginx 等 HTTP 服务器,直接面向前端开发 JavaScript 是由 ECMAScript.文档对象模型(DOM)和浏览器对象模型(B ...
- js函数声明和函数表达式的区别
Javascript Function无处不在,而且功能强大!通过Javascript函数可以让JS具有面向对象的一些特征,实现封装.继承等,也可以让代码得到复用.但事物都有两面性,Javascrip ...
- es7 async/await使用
先创建一个promise对象,里面执行一个异步函数 function fetchUser() { return new Promise((resolve, reject) => { fetch( ...