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. Linux 系统监控.诊断工具之 IO wait

    1. 常用组合方式有如下几种: 用vmstat.sar.iostat检测是否是CPU瓶颈 用free.vmstat检测是否是内存瓶颈 用iostat.dmesg 检测是否是磁盘I/O瓶颈 用netst ...

  2. eclipse中run as无run as server选项的解决方案

    在项目->右击->Properties->Project Facets->Modify Project,选择Java和DynamicWeb Module

  3. HUD 2031: 进制转换

    进制转换 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  4. java的多生产者多消费者例子

    import java.util.concurrent.locks.*; public class Test9 { public static void main(String[] args) { / ...

  5. C/C++笔记之char *与wchar_t *的相互转换

    char *和wchar_t *的相互转换,可使用标准库函数 size_t mbstowcs(wchar_t *wcstr, const char *mbstr, size_t count)和size ...

  6. 在MFC中使用大漠插件

    打开Class Wizard,Add Class...->MFC Class From TypeLib... File->Location->>> Finish-> ...

  7. TEdit的创建与显示过程

    -------------------------- 分析TEdit的创建与显示过程 --------------------------TCustomEdit = class(TWinControl ...

  8. Java服务器端 API 错误码设计总结

    1.对于API结果返回,定义BaseResult 类 拥有success,errorCode,errorMsg个3个基本参数,success使用Boolean类型,errorCode使用Integer ...

  9. docker: Dockerfile命令介绍

    前一章介绍了Dockerfile创建镜像的方法,Dockerfile文件都是一些指令,因此要掌握Dockerfile就必须了解这些指令.这一章就介绍下Dockerfile的指令. From: 功能为指 ...

  10. linux apache 用户认证:

    root@ubuntu:/# htpasswd -c /etc/apache2/password zhangsan (-c表示要创建一个password密码文件,文件存放目录是/etc/apache2 ...