描述
Note: This is a companion problem to the System Design problem: Design TinyURL-System/).
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.
分析
实现一个短链接服务,需要实现编码,解码方法,实际上就是写一个哈希算法,貌似没多少测试数据进行判断,居然还是一道 Medium 难度的题。
解决方案1(Java)
直接使用字符串的 hashCode 函数实现。
1 | public class Codec { |