▶ 要求给出一种对 URL 网址进行压缩和解压的算法,例如 https://leetcode.com/problems/design-tinyurl ←→ http://tinyurl.com/4e9iAk,在不允许打表的情况下应该无法压缩的。

● 代码,6 ms,完全不压缩,just for fun

 class Solution
{
public:
// Encodes a URL to a shortened URL.
string encode(string longUrl) { return longUrl; }
// Decodes a shortened URL to its original URL.
string decode(string shortUrl) { return shortUrl; }
};
 class Solution
{
public:
string theLatestUrl;
string encode(string longUrl) { theLatestUrl = longUrl; return "whatever, doesn't matter"; }
string decode(string shortUrl) { return theLatestUrl; }
};

● 代码,9 ms,随机数打表法,加密之后的字符串只与原网址字符串在表中的存放位置有关,与其内容无关。类似的有各种打表法,需要依赖散列表来解码,时间复杂度 O(1),空间开销大。

 class Solution
{
public:
unordered_map<string, string> um;
string encode(string longUrl)
{
const string baseUrl = "http://tinyurl.com/";
int r;
string ran;
srand();
for (r = rand() % , ran = to_string(r); um.count(baseUrl + ran) == ; r = rand() % , ran = to_string(r));
// 随机取一个空的位置把网址放进去
um[baseUrl + ran] = longUrl;
return baseUrl + ran;
}
string decode(string shortUrl)
{
if (um.count(shortUrl) == )
return um[shortUrl];
return "";
}
};

535. Encode and Decode TinyURL的更多相关文章

  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

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

  4. 535. Encode and Decode TinyURL 长短URL

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

  5. 535. Encode and Decode TinyURL(rand and srand)

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

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

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

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

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

  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. HDU1698 线段树入门之区间修改/查询(lazy标记法)

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  2. Leetcode 18

    class Solution { public: vector<vector<int>> fourSum(vector<int>& nums, int ta ...

  3. $'\r': command not found in Cygwin

    这是因为windows下的回车符改为Unix下的回车符,用以下命令可以批量修改. dos2unix ./*

  4. Big Table中文翻译

    题记:google 的成功除了一个个出色的创意外,还因为有 Jeff Dean 这样的软件架构天才. 官方的 Google Reader blog 中有对BigTable 的解释.这是Google 内 ...

  5. Struts2基本使用(三)--数据交互

    Struts2中的数据交互 在Struts2中我们不必再使用request.getParameter()这种方式来获取前台发送到服务器的参数. 我们可以在服务器端的Java类中直接声明一个和前台发送数 ...

  6. docx文件怎样打开 - 转

    如何打开docx文件?在office2007及2010退出多年后,诸如docx.xlsx.pptx类文件越来越多,我们从网络下载或者别人复制过来的这类文件越来越多.docx文件怎样打开呢?下面有图小站 ...

  7. I.MX6 Manufacturing Tool V2 (MFGTool2) Emmc mksdcard-android.sh hacking

    #!/bin/bash # 参考文章: # . Shell特殊变量:Shell $, $#, $*, $@, $?, $$和命令行参数 # http://c.biancheng.net/cpp/vie ...

  8. Cannot find name 'AsyncIterator' error in Typescript compilation process 问题解决

    解决方法: tsconfig.json: 添加lib 编译选项 { "compilerOptions": { "lib":[ "esnext.asyn ...

  9. Jenkins在windows环境下安装无法安装插件

    在windos平台下安装jenkins要是无法安装插件,tomcat控制台报以下错误: 解决方法: 进入到jenkins里头,Jenkins -- 管理插件 -- 高级 -- 升级站点,如图所示: 将 ...

  10. HDFS 和 YARN 的 HA 故障切换【转】

    来源:https://blog.csdn.net/u011414200/article/details/50336735 一 非 HDFS HA 集群转换成 HA 集群二 HDFS 的 HA 自动切换 ...