535. Encode and Decode TinyURL - LeetCode
Question
535. Encode and Decode TinyURL

Solution
题目大意:实现长链接加密成短链接,短链接解密成长链接
思路:加密成短链接+key,将长链接按key保存到map,解密时根据短链接提取key,再从map中返回长链接
Java实现:
public class Codec {
// https://leetcode.com/problems/design-tinyurl --> http://tinyurl.com/4e9iAk
Map<Integer, String> map = new HashMap<>();
int i = 0;
// Encodes a URL to a shortened URL.
public String encode(String longUrl) {
map.put(i, longUrl);
return "http://tinyurl.com/" + (i++);
}
// Decodes a shortened URL to its original URL.
public String decode(String shortUrl) {
int key = Integer.parseInt(shortUrl.substring(shortUrl.lastIndexOf("/") + 1));
return map.get(key);
}
}
// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.decode(codec.encode(url));
Ref
Using simple counter
public class Codec {
Map<Integer, String> map = new HashMap<>();
int i=0;
public String encode(String longUrl) {
map.put(i,longUrl);
return "http://tinyurl.com/"+i++;
}
public String decode(String shortUrl) {
return map.get(Integer.parseInt(shortUrl.replace("http://tinyurl.com/", "")));
}
}
Using hashcode
public class Codec {
Map<Integer, String> map = new HashMap<>();
public String encode(String longUrl) {
map.put(longUrl.hashCode(),longUrl);
return "http://tinyurl.com/"+longUrl.hashCode();
}
public String decode(String shortUrl) {
return map.get(Integer.parseInt(shortUrl.replace("http://tinyurl.com/", "")));
}
}
Using random function
public class Codec {
Map<Integer, String> map = new HashMap<>();
Random r=new Random();
int key=r.nextInt(10000);
public String encode(String longUrl) {
while(map.containsKey(key))
key= r.nextInt(10000);
map.put(key,longUrl);
return "http://tinyurl.com/"+key;
}
public String decode(String shortUrl) {
return map.get(Integer.parseInt(shortUrl.replace("http://tinyurl.com/", "")));
}
}
535. Encode and Decode TinyURL - LeetCode的更多相关文章
- 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 ...
- 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 ...
- 535. Encode and Decode TinyURL
▶ 要求给出一种对 URL 网址进行压缩和解压的算法,例如 https://leetcode.com/problems/design-tinyurl ←→ http://tinyurl.com/4e9 ...
- 535 Encode and Decode TinyURL 编码和解码精简URL地址
详见:https://leetcode.com/problems/encode-and-decode-tinyurl/description/ C++: class Solution { public ...
- [LeetCode] Encode and Decode TinyURL 编码和解码精简URL地址
Note: This is a companion problem to the System Design problem: Design TinyURL. TinyURL is a URL sho ...
随机推荐
- 雅虎WEB前端网站优化—34条军规
Yslow工具 1.Minimize HTTP Requests 减少HTTP请求 图片.css.script.flash等等这些都会增加http请求数,减少这些元素的数量就能减少响应时间.把多个JS ...
- C++ | 程序编译连接原理
文章目录 预编译(生成*.i文件) 编译(生成*.s文件) 汇编(生成*.o文件,也叫目标文件) 链接(生成*.exe文件,也叫可执行文件) 汇编--目标文件 查看文件头 查看符号表 查看 .o 文件 ...
- css样式权重优先级,css样式优先级
原文:http://www.bkjia.com/Javascri... 样式选择器权重优先级: important > 内嵌样式 > ID > 类 > 标签 | 伪类 | 属性 ...
- WPF控件大全(表格)-学习总结
Label标签 label控件:一般用户描述性文字显示. 在Label控件使用时,一般给予用户提示.用法上没有什么很特殊的,label控件的值记住:不是Text 而是 Content属性. TextB ...
- Java 请求转发和重定向的区别以及JavaWeb三大作用域
三大作用域以及转发和重定向 学习总结 1. 转发和重定向 转发 重定向 转发和重定向的区别: 什么时候用转发什么时候用重定向 三大作用域 作用域类型 作用域方法 如何选择作用域 总结 学习总结 1. ...
- ps让图片背景透明
效果图: jpg=>png,背景透明 步骤: 1.选择橡皮工具的第三个 魔术橡皮 保存为png, 按住Ctrl+alt+shift+s 保存:
- 将对象push到数组中组成对象数组
let items = { key:'', value:'' } for(let i = 0;i<len;i++){ items.value = _this.ills[i].sName; ite ...
- 《头号玩家》AI电影调研报告(二)
四. 涉及前沿技术及与现实的交互 1.VR技术 在影片中,斯皮尔伯格用他认为未来的VR虚拟技术为我们创造了众多精彩的画面,令人佩服其对科技的预见性.其中好多的装备特别引人注目,部分也在现实中存在:VR ...
- 如何在云服务器上安装vim(bash: vim :command not found)
1.apt-get update 2.apt-get install vim vim文件即可成功!
- 使用SQL的FOR XML PATH('')将字段用逗号隔开
FOR XML PATH('') 将查询结果显示为XML 经常用来将查询结果按逗号分隔后显示至某一字段 select * from Area结果 添加FOR XML PATH('')后 select ...