Encode and Decode 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.
解法来自:https://leetcode.com/problems/encode-and-decode-tinyurl/discuss/100270/Three-different-approaches-in-java
Approach 1- Using simple counter
public class Codec {
Map<Integer, String> map = new HashMap<>();
int i=;
public String encode(String longUrl) {
map.put(i,longUrl);
return "http://tinyurl.com/"+i++;
}
public String decode(String shortUrl) {
return map.get(Integer.parseInt(shortUrl.replace("http://tinyurl.com/", "")));
}
}
Approach 2- using hashcode
public class Codec {
Map<Integer, String> map = new HashMap<>();
public String encode(String longUrl) {
map.put(longUrl.hashCode(),longUrl);
return "http://tinyurl.com/"+longUrl.hashCode();
}
public String decode(String shortUrl) {
return map.get(Integer.parseInt(shortUrl.replace("http://tinyurl.com/", "")));
}
Approach 3- using random function
public class Codec {
Map<Integer, String> map = new HashMap<>();
Random r=new Random();
int key=r.nextInt();
public String encode(String longUrl) {
while(map.containsKey(key)) {
key= r.nextInt();
}
map.put(key,longUrl);
return "http://tinyurl.com/"+key;
}
public String decode(String shortUrl) {
return map.get(Integer.parseInt(shortUrl.replace("http://tinyurl.com/", "")));
}
}
Encode and Decode TinyURL的更多相关文章
- LC 535. Encode and Decode TinyURL
Note: This is a companion problem to the System Design problem: Design TinyURL. TinyURL is a URL sho ...
- 535. Encode and Decode TinyURL - LeetCode
Question 535. Encode and Decode TinyURL Solution 题目大意:实现长链接加密成短链接,短链接解密成长链接 思路:加密成短链接+key,将长链接按key保存 ...
- [LeetCode] Encode and Decode TinyURL 编码和解码精简URL地址
Note: This is a companion problem to the System Design problem: Design TinyURL. TinyURL is a URL sho ...
- Leetcode: 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 ...
随机推荐
- Nvidia和Google的AI芯片战火蔓延至边缘端
AI 的热潮还在持续,AI 的战火自然也在升级.英伟达作为这一波 AI 浪潮中最受关注的公司之一,在很大程度上影响着 AI 的战局.上周在美国举行的 GTC 2019 上,黄仁勋大篇幅介绍了英伟达在 ...
- 嵌入式操作系统---打印函数(printf/sprintf)的实现
一.打印函数简介 作用:将“给定的内容”按照“指定的格式”输出到“指定目标内”. 打印函数的基本格式: char print_buf[BUF_SIZE]; void printf(const char ...
- 【原创】IDEA一定要改的八条配置
引言 坦白说,我很少写这种操作类型的文章.因为这种文章没啥新意,大家操作步骤肯定是一样的.然而,我答应了我的同事小阳,给她出一篇!毕竟人家打算从Eclipse转IDEA了,于是以示鼓励,写一篇给她! ...
- 访问网站出现 HTTP ERROR 500 该网页无法正常运作
项目在本地环境配置好后访问出现如下图所示: 经过查看php日志文件发现问题在于数据库连接错误,如下图: 修改成本地的数据库用户名和密码,重启服务器即可正常访问.
- Windows 2016 忘记密码的处理方法
发现使用 osk 还有 magnify 的方式修改 密码的方式在win server 的机器上面行不通了. 换一种方式进行处理. 使用PE 方式处理. 1. 下载PE 发现比较早的PE 也搞不定 可能 ...
- webservice异常
webservice的一个常见异常: [SOAPException: faultCode=SOAP-ENV:Client; msg=Error parsing HTTP status line &qu ...
- linux的挂载含义
Linux下,mount挂载的作用,就是将一个设备(通常是存储设备)挂接到一个已存在的目录上.访问这个目录就是访问该存储设备.linux操作系统将所有的设备都看作文件,它将整个计算机的资源都整合成一个 ...
- python 爬取可用
#coding:utf-8 from bs4 import BeautifulSoup import time import threading import random import telnet ...
- Batch Normalization原理
Batch Normalization导读 博客转载自:https://blog.csdn.net/malefactor/article/details/51476961 作者: 张俊林 为什么深度神 ...
- Python 面向对象高阶-----metaclass
Python 面向对象高阶-----metaclass 前言 类也是对象,既然类是对象,那就自然是某个东西的实例化,这个东西就是type 首先看下type是怎么回事 type type最常用的方法就是 ...