Note: This is a companion problem to the System Design problem: Design TinyURL.

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.

Approach #1: C++.

class Solution {
public: // Encodes a URL to a shortened URL.
string encode(string longUrl) {
if (longToTiny.count(longUrl)) return baseUrl + longToTiny[longUrl];
string tinyString = "";
do {
for (int i = 0; i < 6; ++i) {
//srand((unsigned)time(0));
int index = rand() % characters.length();
tinyString += characters[index];
}
} while (longToTiny.count(tinyString)); longToTiny[longUrl] = tinyString;
tinyToLong[baseUrl+tinyString] = longUrl; return baseUrl + tinyString;
} // Decodes a shortened URL to its original URL.
string decode(string shortUrl) {
return tinyToLong[shortUrl];
} private:
string baseUrl = "http://www.leetcode.com/";
string characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
unordered_map<string, string> longToTiny;
unordered_map<string, string> tinyToLong;
}; // Your Solution object will be instantiated and called as such:
// Solution solution;
// solution.decode(solution.encode(url));

  

Approach #2: Python.

class Codec:
alphabet = string.ascii_letters + '0123456789' def __init__(self):
self.url2code = {}
self.code2url = {} def encode(self, longUrl):
"""Encodes a URL to a shortened URL. :type longUrl: str
:rtype: str
"""
while longUrl not in self.url2code:
code = ''.join(random.choice(Codec.alphabet) for _ in range(6))
if code not in self.code2url:
self.code2url[code] = longUrl
self.url2code[longUrl] = code
return 'http://tinyurl.com/' + self.url2code[longUrl] def decode(self, shortUrl):
"""Decodes a shortened URL to its original URL. :type shortUrl: str
:rtype: str
"""
return self.code2url[shortUrl[-6:]] # Your Codec object will be instantiated and called as such:
# codec = Codec()
# codec.decode(codec.encode(url))

  

Time Submitted Status Runtime Language
a few seconds ago Accepted 28 ms python
2 minutes ago Accepted 36 ms python
13 minutes ago Accepted 8 ms cpp

535. Encode and Decode TinyURL(rand and srand)的更多相关文章

  1. 535. Encode and Decode TinyURL - LeetCode

    Question 535. Encode and Decode TinyURL Solution 题目大意:实现长链接加密成短链接,短链接解密成长链接 思路:加密成短链接+key,将长链接按key保存 ...

  2. LC 535. Encode and Decode TinyURL

    Note: This is a companion problem to the System Design problem: Design TinyURL. TinyURL is a URL sho ...

  3. [LeetCode] 535. Encode and Decode TinyURL 编码和解码短网址

    Note: This is a companion problem to the System Design problem: Design TinyURL. TinyURL is a URL sho ...

  4. 【Leetcode】535. Encode and Decode TinyURL

    Question: TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/pro ...

  5. 535. Encode and Decode TinyURL 长短URL

    [抄题]: TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problem ...

  6. 【LeetCode】535. Encode and Decode TinyURL 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 方法一:数组 方法二:字典 日期 题目地址:https://l ...

  7. 535. Encode and Decode TinyURL

    ▶ 要求给出一种对 URL 网址进行压缩和解压的算法,例如 https://leetcode.com/problems/design-tinyurl ←→ http://tinyurl.com/4e9 ...

  8. 535 Encode and Decode TinyURL 编码和解码精简URL地址

    详见:https://leetcode.com/problems/encode-and-decode-tinyurl/description/ C++: class Solution { public ...

  9. [LeetCode] Encode and Decode TinyURL 编码和解码精简URL地址

    Note: This is a companion problem to the System Design problem: Design TinyURL. TinyURL is a URL sho ...

随机推荐

  1. Java中Class.forName()的作用(转载)

    http://www.360doc.com/content/10/0712/10/1720440_38421273.shtml# 使用jdbc方式连接数据库时会使用一句代码Class.forName( ...

  2. ASP.Net MVC upload file with record & validation - Step 6

    Uploading file with other model data, validate model & file before uploading by using DataAnnota ...

  3. Nginx下的https配置

    https: https(Secure Hypertext Transfer Protocol) 安全超文本传输协议 它是以安全为目标的http通道,即它是http的安全版.它使用安全套接字层(SSL ...

  4. compute the su procedure time with python

    #!/usr/bin/python2.6 import re,datetime file_name='sim.log' file=open(file_name,'r') acnum=[];time_r ...

  5. 【百度之星初赛A】路径交 LCA+线段树

    [百度之星初赛A]路径交 Problem Description 给定一棵n个点的树,以及m条路径,每次询问第L条到第R条路径的交集部分的长度(如果一条边同时出现在2条路径上,那么它属于路径的交集). ...

  6. 理解 React,但不理解 Redux,该如何通俗易懂的理解 Redux?

    作者:Wang Namelos链接:https://www.zhihu.com/question/41312576/answer/90782136来源:知乎著作权归作者所有.商业转载请联系作者获得授权 ...

  7. ElasticSearch(十一)批量CURD bulk

    1.bulk语法 POST /_bulk { "delete": { "_index": "test_index", "_type ...

  8. Xcode工程断点调试失效

    1.我解决的是方法是,选择Product---->Edit Scheme------>(这里进入后会有Info,Arguments,Options,Diagnostics)选择Info-- ...

  9. 单元测试JUnit4 Ctrl + Shift + T

    单元测试 public class Calculator { public int result = 0; public int add(int operandl, int operand2) { r ...

  10. Linux档案属性

    输入命令:ls -al 档案类型权限: 第一個字元代表这个档案是『目录.档案或链接档等等』: 当为[ d ]则是目录: 当为[ - ]则是目录: 若是[ l ]则表示为链接档(link file): ...