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的Web层中实现复杂请求跨域访问
在最近的项目中,后端使用ABP,前端采用React,前后端完全分离.其中大部分接口都通过WebApi层调用,项目中未使用Session.但最后在添加一个网站的验证码验证留言功能时,使用了Session ...
- c&c++中的宏
1 c&c++中的宏 do {...} while (0); offsetof & container_of 2 引用 [1] do {...} while (0) 在宏定义中的作用 ...
- Grafana和influxdb监控nginx日志中的请求响应时间图形化监控
监控效果如图: 监控方法: 通过logstash过滤nginx日志,然后解析出nginx日志中的request time字段 然后output到influxdb时序数据库中 通过grafana展示数据 ...
- Laravel日常使用总结
字段当做键值key 集合的keyBy()方法详情:戳这里 创建模型类和数据库迁移文件 php artisan make:model Post -m 创建控制器和基础的方法 php artisan ma ...
- 自学华为IoT物联网_08 IoT连接管理平台介绍
点击返回自学华为IoT物流网 自学华为IoT物联网_08 IoT连接管理平台介绍 一.IoT连接管理平台的由来 1.1 物联网产业发展面临的挑战 新业务上线周期长,应用碎片化,开发周期长,场频上市慢 ...
- 小甲鱼Python第二十一讲课后习题
测试题: 0. 递归在编程上的形式是如何表现的呢? 在编程上,递归表现为函数调用本身这么一个行为. 1. 递归必须满足哪两个基本条件? 一. 函数调用自身二. 设置了正 ...
- Linux下的文件切割和文件合并
linux下文件分割可以通过split命令来实现,可以指定按行数分割和按大小分割两种模式.Linux下文件合并可以通过cat命令来实现. 在Linux下用split进行文件分割: ①:指定分割后文件行 ...
- Java Base64 加解密
public class base64EncryAndDecry { public static final String CODES = "ABCDEFGHIJKLMNOPQRSTUVWX ...
- WinIo驱动级键盘模拟编程
转自:http://blog.sina.com.cn/s/blog_455d7a320100vr37.html 前天无聊,翻翻自己的兴趣项目文件夹,发现了这个放下很久的项目!那是大三时候的事了.当时是 ...
- vue发送请求---fetch-jsonp
fetch-jsonp和axios类似,都是第三方插件返送请求,而vue-resource是vue官方提供的请求插件 前两个哪个组件使用就在那里引入,vue-resource直接在vue的main.j ...