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 ...
随机推荐
- ABP给WebApi添加性能分析组件Miniprofiler
在ABP的WebApi中,对其性能进行分析监测是很有必要的.而悲剧的是,MVC项目中可以使用的MiniProfiler或Glimpse等,这些都不支持WebApi项目,而且WebApi项目通常也没有界 ...
- Ubuntu16.04下Hadoop的本地安装与配置
一.系统环境 os : Ubuntu 16.04 LTS 64bit jdk : 1.8.0_161 hadoop : 2.6.4 部署时使用的用户名为hadoop,下文中需要使用用户名的地方请更改为 ...
- 此处为当前页,设置此处的href点后没有效果
<%--此处当前页不能点,设置href为没有动作Javascript:void(0); --%> 如果javaScript:void(0);写错了,那就很尴尬(某些浏览器忽略该错误如:谷歌 ...
- 为什么Dotnet Core的DI默认是在控制器中注入
转载请注明出处: https://www.cnblogs.com/zhiyong-ITNote/ DI的大概过程抽象成如下:接口对象 <-- 实现对象 <-- 抽象对象 在抽象对象中引入接 ...
- C#-导入Excel 内容到 DataTable中
C#-导入Excel 内容到 DataTable中 直接传入文件路径,支持所有Excel格式. 缺点:如果数据量庞大会很占内存. public static DataTable ImportExcel ...
- docker+ubuntu14.04+cuda7.0
参考链接: http://tleyden.github.io/blog/2014/10/25/docker-on-aws-gpu-ubuntu-14-dot-04-slash-cuda-6-dot-5 ...
- java单链表反转(花了半个多小时的作品)
欢迎光临............... 首先我们要搞清楚链表是啥玩意儿?先看看定义: 讲链表之前我们先说说Java内存的分配情况:我们new对象的时候,会在java堆中为对象分配内存,当我们调用方法的 ...
- java判断通常的逻辑
package com.stylefeng.guns.core.common.constant.factory; import com.baomidou.mybatisplus.mapper.Enti ...
- 6. 深度克隆_ES7**_arr.includes('孙悟空')
1. 如何实现深度克隆 利用 JSON 方法 (没办法克隆函数数据) `JSON.parse(JSON.stringify(xxx))` 自定义方法 检查所有数据类型的方法 `Object.proto ...
- if-else案例–开关灯
首先,创建一个html页面,添加一个div盒子,用css设置相应的样式,用js获取盒子的元素,通过点击事件,设置body的背景颜色,用if..else来判断当什么状态设置相应的颜色,(swith... ...