535. Encode and Decode TinyURL
▶ 要求给出一种对 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的更多相关文章
- 535. Encode and Decode TinyURL - LeetCode
Question 535. Encode and Decode TinyURL Solution 题目大意:实现长链接加密成短链接,短链接解密成长链接 思路:加密成短链接+key,将长链接按key保存 ...
- LC 535. Encode and Decode TinyURL
Note: This is a companion problem to the System Design problem: Design TinyURL. TinyURL is a URL sho ...
- 【Leetcode】535. Encode and Decode TinyURL
Question: TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/pro ...
- 535. Encode and Decode TinyURL 长短URL
[抄题]: TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problem ...
- 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 ...
- [LeetCode] 535. Encode and Decode TinyURL 编码和解码短网址
Note: This is a companion problem to the System Design problem: Design TinyURL. TinyURL is a URL sho ...
- 【LeetCode】535. Encode and Decode TinyURL 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 方法一:数组 方法二:字典 日期 题目地址:https://l ...
- 535 Encode and Decode TinyURL 编码和解码精简URL地址
详见:https://leetcode.com/problems/encode-and-decode-tinyurl/description/ C++: class Solution { public ...
- [LeetCode] Encode and Decode TinyURL 编码和解码精简URL地址
Note: This is a companion problem to the System Design problem: Design TinyURL. TinyURL is a URL sho ...
随机推荐
- IOS-网络(文件上传)
// // ViewController.m // IOS_0206_文件上传 // // Created by ma c on 16/2/6. // Copyright © 2016年 博文科技. ...
- 045——VUE中组件之父组件使用scope定义子组件模板结构
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- POJ 2251 Dungeon Master bfs 难度:0
http://poj.org/problem?id=2251 bfs,把两维换成三维,但是30*30*30=9e3的空间时间复杂度仍然足以承受 #include <cstdio> #inc ...
- 线程的同步之Synchronized在单例模式中的应用
synchronized在单例模式中的使用 在单例模式中有一种懒汉式的单例,就是类初始化的时候不创建对象.等第一次获取的时候再创建对象.这种单例在单线程下是没有问题的获取的也都是同一个对象.但是如果放 ...
- redux-thunk中间件源码
浅析redux-thunk中间件源码 大多redux的初学者都会使用redux-thunk中间件来处理异步请求,其理解简单使用方便(具体使用可参考官方文档).我自己其实也一直在用,最近偶然发现其源码只 ...
- Java中替换字符串中特殊字符+ 20150921
需求:今天需要将字符串中的" +"转换程"%2B",但是"+"是正则表达式中的特殊字符,使用需要反斜杠转义,具体示范: String a=& ...
- C/C++ 父子进程之间的文件描述符问题
在C程序中,文件由文件指针或者文件描述符表示.ISO C的标准I/0库函数(fopen, fclose, fread, fwrite, fscanf, fprintf等)使用文件指针,UNIX的I/O ...
- C/C++函数中使用可变参数
先说明可变参数是什么,先回顾一下C++里面的函数重载,如果重复给出如下声明: int func(); int func(int); int func(float); int func(int, int ...
- 通过iframe 实现upload file无刷新
<html> <head> </head> <body> <form encType="multipart/form-data&q ...
- ASP.NET MVC 使用NPOI导出Excel 无法访问已关闭的流(转)
第一步重写MemoryStream , 让它不能自动关闭. //新建类 重写Npoi流方法 public class NpoiMemoryStream : MemoryStream { public ...