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. WPF触发器(Trigger、DataTrigger、EventTrigger)

    WPF中有种叫做触发器的东西(记住不是数据库的trigger哦).它的主要作用是根据trigger的不同条件来自动更改外观属性,或者执行动画等操作. WPFtrigger的主要类型有:Trigger. ...

  2. Darwin Streaming Server编译

    EasyDarwin是我们在Darwin Streaming Server的基础上进行开发和扩展的,Windows/Linux编译,包括64位编译补丁,我们都进行了整理和测试,后续还会不断进行迭代和扩 ...

  3. difference between http get and post

    1 用get的地方 在浏览器中输入url直接访问资源时,用get.get是通过url传参的. 2 用post的地方 2.1 url长度超限时 post是将参数放在http body中的,因此对参数的长 ...

  4. lazy evaluation and deferring a computation await promise async

    Promise - JavaScript | MDN https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_ ...

  5. Hadoop实战-MapReduce之WordCount(五)

    环境介绍: 主服务器ip:192.168.80.128(master)  NameNode  SecondaryNameNode ResourceManager 从服务器ip:192.168.80.1 ...

  6. python网络爬虫之使用scrapy爬取图片

    在前面的章节中都介绍了scrapy如何爬取网页数据,今天介绍下如何爬取图片. 下载图片需要用到ImagesPipeline这个类,首先介绍下工作流程: 1 首先需要在一个爬虫中,获取到图片的url并存 ...

  7. git branch 分支创建时间排序

    git branch日期排序 vi ~/.gitconfig [alias]lb = !"for k in `git branch -a|perl -pe s/^..//`;do echo ...

  8. jQuery事件函数位置放置的两种方法

    jQuery 事件函数 jQuery 事件处理方法是 jQuery 中的核心函数. 事件处理程序指的是当 HTML 中发生某些事件时所调用的方法. 通常会把 jQuery 代码放到 <head& ...

  9. linux CentOS7.2配置LNMP

    转自http://www.centoscn.com/CentosServer/www/2014/0904/3673.html 准备篇: CentOS 7.0系统安装配置图解教程 http://www. ...

  10. uni-app 创建的第一个应用

    本人微信公众号:前端修炼之路,欢迎关注 背景介绍 经过上一篇文章uni-app官方教程学习手记的学习之后,我就着手做这个项目了. 目前已经初步搭出了整体的框架,秉着取之于社会,回馈于社会的原则,我将这 ...