Leetcode刷题之路(Python)——535. Encode and Decode TinyURL
来源:互联网 发布:matlab 生成稀疏矩阵 编辑:程序博客网 时间:2024/05/18 00:46
题目的链接: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个字符随机排列组合,得到http://tinyurl.com/xxxxxx 中的xxxxxx短码,从而得到一个短的URL:http://tinyurl.com/xxxxxx 代码如下:
import stringletters = string.ascii_letters + string.digitsdef six_addr(): ans='' tmp='' for i in range(6): tmp=letters[random.randint(0,10000)%62] ans=ans+tmp return ans
letters[random.randint(0,10000)%62]
是在62个字符中随机选择一个。这里我写了一个for循环来随机生成6个字符,并ans=ans+tmp
拼在了一起,组成了上面我们要得到的六个字符ans。
- 要怎么对应?长URL与短URL是一一对应的关系。在python中可以创建字典(其他语言中叫做map)去存储它们。在这里要创建两个字典:full_tiny和tiny_full。full_tiny的key是长URL,vaule是短URL。tiny_full则相反。
suffix = six_addr()full_tiny[longUrl]=suffixtiny_full[suffix] = longUrl
- 考虑重复的问题
一个长URL对应一个短URL。如果重复输入长URL,则不会分配新的短URL。所以在分配之前,要看看长URL是否已经存在,如果已经存在,则直接返回现有的短URL,而不再重新分配:
if longUrl in full_tiny: return "http://tinyurl.com/" + full_tiny[longUrl]
- 怎么解码?
首先我们要获得需要解码的东西,也就是我们编码的那六位字符串。那是拼在http://tinyurl.com/ 后面的,所以要先提取出来:shortUrl = shortUrl.split('/')[-1]
。编码时我们创建了tiny_full字典,它的key就是短URL。我们根据短的URL,在tiny_full字典中查找它对应的长URL即可解码。
def decode(self, shortUrl): shortUrl = shortUrl.split('/')[-1] if shortUrl in tiny_full: return tiny_full[shortUrl] else: return None
以下是可执行并ac过的完整代码:
import stringimport randomimport mathfull_tiny = {}tiny_full = {}letters = string.ascii_letters + string.digitsclass Codec: def encode(self, longUrl): """Encodes a URL to a shortened URL. :type longUrl: str :rtype: str """ def six_addr(): ans='' tmp='' for i in range(6): tmp=letters[random.randint(0,10000)%62] ans=ans+tmp return ans if longUrl in full_tiny: return "http://tinyurl.com/" + full_tiny[longUrl] else: suffix = six_addr() full_tiny[longUrl]=suffix tiny_full[suffix] = longUrl return "http://tinyurl.com/" + suffix def decode(self, shortUrl): """Decodes a shortened URL to its original URL. :type shortUrl: str :rtype: str """ shortUrl = shortUrl.split('/')[-1] if shortUrl in tiny_full: return tiny_full[shortUrl] else: return None# Your Codec object will be instantiated and called as such:# codec = Codec()# codec.decode(codec.encode(url))
ac截图:
GOOD LUCK FOR YOU ~
memoryjdch编辑于2017.10.17
- Leetcode刷题之路(Python)——535. Encode and Decode TinyURL
- python--leetcode 535. Encode and Decode TinyURL
- leetcode题解-535. Encode and Decode TinyURL
- 【LeetCode】535. Encode and Decode TinyURL
- LeetCode:535. Encode and Decode TinyURL
- Leetcode 535. Encode and Decode TinyURL
- leetcode 535. Encode and Decode TinyURL
- Leetcode 535. Encode and Decode TinyURL
- LeetCode 535. Encode and Decode TinyURL
- 【LeetCode】535. Encode and Decode TinyURL
- leetcode 535. Encode and Decode TinyURL
- LeetCode-Encode and Decode TinyURL
- 535. Encode and Decode TinyURL
- 535. Encode and Decode TinyURL
- 535. Encode and Decode TinyURL
- 535. Encode and Decode TinyURL
- 535. Encode and Decode TinyURL
- 535. Encode and Decode TinyURL
- MySQL学习(一)
- TCP 协议 三次握手 四次挥手 数据传输
- java基础大汇总一
- pandas画图
- 任学堂讲科技:“进击的AI”,人工智能未来将是人类头号威胁?
- Leetcode刷题之路(Python)——535. Encode and Decode TinyURL
- 爬取frame内的多条链接
- vue
- 安装tomcat
- linked-list-cycle-ii Java code
- java与mysql时间类型对应(转载,未验证)
- Ubuntu经验/技巧
- scrapy 知乎模拟登录
- 反射机制reflect