题目的链接:https://leetcode.com/problems/encode-and-decode-tinyurl/description/
“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.”
TinyUrl是一个精简URL的服务器。功能就是输入一个URL,例如:https://leetcode.com/problems/design-tinyurl 它会返回一个精简后的地址,例如:http://tinyurl.com/4e9iAk.
我们的任务是要设计encode(编码)和decode(解码)这两个函数。对于两个函数的工作原理不做限制,我们只用保证的是:一个URL可以被编码为一个简短的URL,并且这个间断的URL可以被解码为原来的URL。
解题思路
我们的主要任务是写两个函数:encode、decode。先把encode编好,decode根据encode的短URL,直接可以读取原始的URL。对于encode要考虑几个问题:
- 短URL怎么来?
我们可以取26个字母,大小写敏感,10个数字。这样总共有62个字符。这62个字符随机排列组合,