【Leetcode】535. Encode and Decode TinyURL
Question:
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.
Tips:
将长的url转换为短url 将短url转换为长的url。长url与短url之间有一个映射,保证一致。
如输入https://leetcode.com/problems/design-tinyurl 编码之后返回http://tinyurl.com/4e9iAk
输入http://tinyurl.com/4e9iAk,decode之后也要返回https://leetcode.com/problems/design-tinyurl。
编码方式没有限制,主要是code与decode之后 结果要相同。
思路:
code:
输入类似:https://leetcode.com/problems/design-tinyurl
用longUrl的哈希值作为hashmap的key,longUrl作为value,组成键值对,保存在map中。
返回"http://tinyurl.com/"+longUrl的哈希值,即键值。
decode:
输入类似:http://tinyurl.com/4e9iAk
根据http://tinyurl.com/ 后面的4e9iAk作为key 来查找对应的value就是longUrl
代码:(使用hashmap)
package medium; import java.util.HashMap;
import java.util.Map; public class L535EncodeAndDecodeTinyURL {
//即将长的url转换为短url 将短url转换为长的url。
//长url与短url之间有一个映射,保证一致。
//如输入https://leetcode.com/problems/design-tinyurl 编码之后返回http://tinyurl.com/4e9iAk
//输入http://tinyurl.com/4e9iAk,decode之后也要返回https://leetcode.com/problems/design-tinyurl
Map<Integer, String> map = new HashMap<>(); // Encodes a URL to a shortened URL.
public String encode(String longUrl) {
map.put(longUrl.hashCode(), longUrl);
//System.out.println("long hashCode"+longUrl.hashCode());
return "http://tinyurl.com/" + longUrl.hashCode();
} // Decodes a shortened URL to its original URL.
public String decode(String shortUrl) {
return map.get(Integer.parseInt(shortUrl.replace("http://tinyurl.com/", "")));
} // Your Codec object will be instantiated and called as such:
public static void main(String[] args) {
L535EncodeAndDecodeTinyURL l535 = new L535EncodeAndDecodeTinyURL();
System.out.println(l535.decode(l535.encode("https://leetcode.com/problems/design-tinyurl")));
}
}
【Leetcode】535. Encode and Decode TinyURL的更多相关文章
- 【LeetCode】535. Encode and Decode TinyURL 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 方法一:数组 方法二:字典 日期 题目地址:https://l ...
- 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 编码和解码短网址
Note: This is a companion problem to the System Design problem: Design TinyURL. TinyURL is a URL sho ...
- 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】91. Decode Ways 解题报告(Python)
[LeetCode]91. Decode Ways 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...
随机推荐
- Android Studio中新建和引用assets文件
从eclipse转过的朋友们应该不太习惯AS中新建assets文件和对文件内容的引用.我也查找了网上很多资料发现很少有这样的解决答案,于是便把自己解决的方法总结在这里. 1.一般新建project后这 ...
- mapreduce设置setMapOutputKeyClass与setMapOutputValueClass原因
一般的mapreduce的wordcount程序如下: public class WcMapper extends Mapper<LongWritable, Text, Text, LongWr ...
- gatewayworker中使用tcp协议连接硬件设备获取数据报错解决办法!
运行后过段时间报错, Warning: Error while sending STMT_PREPARE packet. PID=1776 in D:\phpStudy\WWW\api\mysql-m ...
- Linux设置口令复杂度和口令定期更换策略
Linux 密码复杂度设置pam_pwquality.pam_passwdqc(centos7) 1.Linux对应的密码策略模块有:pam_passwdqc 和 pam_pwquality . 其中 ...
- sublime出现 unable download.......
I managed to fix this by changing my package settings. I made my osx downloader preference curl, and ...
- debian文本配置网络备忘:/etc/network/interfaces
我装了wheezy有gnome3,xfce4: 郁闷的是,不论在gnome还是xfce4中 我都无法图形登录或者切换用户到root: 而且我无法在普通用户下图形修改网络配置: 我也搜索不到启用root ...
- vector使用小结
1.创建vector容器: std::vector<int> data; std::vector<int> data(20);大小20,自动赋值为0 std::vector&l ...
- JS时间轴效果(类似于qq空间时间轴效果)
在上一家公司写了一个时间轴效果,今天整理了下,感觉有必要写一篇博客出来 给大家分享分享 当然代码还有很多不足的地方,希望大家多指点指点下,此效果类似于QQ空间或者人人网空间时间轴效果,当时也是为了需求 ...
- 609E- Minimum spanning tree for each edge
Connected undirected weighted graph without self-loops and multiple edges is given. Graph contains n ...
- [CF1039D]You Are Given a Tree[贪心+根号分治]
题意 给你\(n\)个点的树,其中一个简单路径的集合被称为\(k\)合法当且仅当树的每个节点最多属于一条路径,且每条路径包含\(k\)个节点.对于每个\(k(k \in [1,n])\),输出最多的\ ...