leetcode535
public class Codec
{
const string alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; Dictionary<string, string> url2code = new Dictionary<string, string>();
Dictionary<string, string> code2url = new Dictionary<string, string>(); const string TINYURL = "http://tinyurl.com/"; // Encodes a URL to a shortened URL
public string encode(string longUrl)
{
while (!url2code.ContainsKey(longUrl))//原来没有这个url的short版本
{
//需要生成一个新的
var random = new Random(DateTime.Now.Millisecond);
StringBuilder sb = new StringBuilder();
while (sb.Length < )
{
var index = random.Next();
sb.Append(alphabet[index]);
}
var code = TINYURL + sb.ToString();
if (!code2url.ContainsKey(code))//新生成的这个code,之前没用过
{
url2code.Add(longUrl, code);
code2url.Add(code, longUrl);
}
}
return url2code[longUrl];
} // Decodes a shortened URL to its original URL.
public string decode(string shortUrl)
{
if (code2url.ContainsKey(shortUrl))
{
return code2url[shortUrl];
}
else
{
return shortUrl;
}
}
} // Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.decode(codec.encode(url));
https://leetcode.com/problems/encode-and-decode-tinyurl/#/description
leetcode535的更多相关文章
随机推荐
- http协议知识整理
HTTP 协议 作为web开发人员,了解一些http协议的知识很有必要.本文简单介绍了HTTP协议的知识,若有错误的地方,望大家指正. 1.HTTP协议是什么? http协议是一个应用层的协议.规定了 ...
- Could not load the "light_rain.png" image referenced from a nib in the bundle with identifier
导入图片文件夹的时候勾选create groups
- 6LowPan 开发之开山篇
本文参考: http://blog.csdn.net/xukai871105/article/details/9204101 1.基本概念 1) instant contikit Ubu ...
- 网络编程(Socket)
引言: 从2007年毕业开始一直从事.net web应用程序开发,负责冶金行业的MES系统开发,写了差不多6年左右的代码,由于工作性质是需要驻厂开发,一直出差,所以在2013年跳槽了,目前从事安防行业 ...
- table中文字过长使用省略号
1.设置table固定布局,否则自适应布局会不受控制 table{ table-layout: fixed; } 2.设定td宽度占比 <table> <col width=&quo ...
- LOJ2325. 「清华集训 2017」小 Y 和恐怖的奴隶主【矩阵快速幂优化DP】【倍增优化】
LINK 思路 首先是考虑怎么设计dp的状态 发现奴隶主的顺序没有影响,只有生命和个数有影响,所以就可以把每个生命值的奴隶主有多少压缩成状态就可以了 然后发现无论是什么时候一个状态到另一个状态的转移都 ...
- Sharepoint Web.config trust level="Full"权限说明
在SharePoint里面,不仅有用户的权限,还有代码的权限.比如,我们在安装一个自定义的WebPart的时候,默认的情况下是不能操纵文件夹的,如果你看一些教你怎么做WebPart的文章的话,你会发现 ...
- bean:write
bean:write相当于<%=request.getAttribute("something")%> 例子一: 某处设置了request.setAttribute(& ...
- k8s helm 私服chartmuseum minio s3 存储配置
1. 安装minio 使用docker 安装 参考项目 https://github.com/rongfengliang/mino-thumbor-openresty 备注: 因为是一个集成项目可能会 ...
- UML中的几种关系(UML Relationships)
依赖(Dependency) 依赖可以理解为一个类A使用到了另一个类B,而这种使用关系是具有偶然性的.临时性的.非常弱的,但是B类的变化会影响到A:比如某人要过河,需要借用一条船,此时人与船之间的关系 ...