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 ...
随机推荐
- 8_根轨迹_Part2_根轨迹手绘技巧
传递函数分母部分相同
- 小程序 wx.getSystemInfoSync 获取 windowHeight 问题
windowHeight 概念 可使用窗口高度,即:屏幕高度(screenHeight) - 导航(tabbar)高度 存在问题 安卓设备下获取 windowHeight 不能准确得到对应的高度,总是 ...
- onActivityResult执行两次问题
差点被坑死 记一次onActivityResult被调用两次的坑
- 将项目导入eclipse中出现的jsp页面报错
图片摘自百度经验,实在是每次都会忘了步骤,每次都得重新百度,所以索性自己总结到博客中,下次如果还记不住就直接从博客中看.原谅我实在学渣,呜呜~~~~(>_<)~~~~
- pip——重新安装
原因 没有用管理员的权限安装.导致失败. 并且pip还被卸载了. 使用环境 win10 重新安装pip python -m ensurepip 更新pip 管理员打开cmd python -m pip ...
- springboot+springsecurity+mybatis plus注解实现对方法的权限处理
文章目录 接上文 [springboot+springsecurity+mybatis plus之用户授权](https://blog.csdn.net/Kevinnsm/article/detail ...
- iwdg和wwdg
一.什么是看门狗? 在单片机工作的时候经常会出现受到外界电磁场的干扰导致程序跑飞,而陷入死循环,而使整个系统陷入无法正常工作的状态. "看门狗"是一种专门用于监测单片机程序运行状态 ...
- Elasticsearch 使用-安装
Elasticsearch 使用-安装 官方网站 https://www.elastic.co/cn/elasticsearch/ 什么是 Elasticsearch? Elasticsearch 是 ...
- linux修改静态ip
1.修改配置文件 vi /etc/sysconfig/network-scripts/ifcfg-ens32 bootproto:设置为静态 onboot:开机自启 ipaddr:ip地址 netma ...
- 基于kubernetes的分布式限流
做为一个数据上报系统,随着接入量越来越大,由于 API 接口无法控制调用方的行为,因此当遇到瞬时请求量激增时,会导致接口占用过多服务器资源,使得其他请求响应速度降低或是超时,更有甚者可能导致服务器宕机 ...