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 ...
随机推荐
- Skyline 7 版本TerraExplorer Pro二次开发快速入门
年底了,给大家整理了一下Skyline 7版本的二次开发学习初级入门教程,献给那些喜欢学习的年轻朋友. 我这整理的是Web控件版本的开发示例,里面页面代码保存成html,都可以直接运行的. 测试使用的 ...
- 2、FreeRTOS任务相关API函数
1.任务相关的API函数 函数存在于task.c中,主要的函数有: xTaskCreate():使用动态的方法创建一个任务: xTaskCreatStatic():使用静态的方法创建一个任务(用的非常 ...
- Auto Layout - BNR
继续UIImageView - BNR篇. 通过Homepwner TARGETS -> General -> Deployment Info -> Devices中的iPhone改 ...
- Mysql_连接字符串
1.本地数据库连接 <connectionStrings> <add name="ConnectionString" connectionString=" ...
- Equinox OSGi应用嵌入Jersey框架搭建REST服务
原文地址:https://www.cnblogs.com/kira2will/p/5040264.html 一.环境 eclipse版本:eclipse-luna 4.4 jre版本:1.8 二.Eq ...
- java基础-不用ide如何打包
java基础-不用ide如何打包 1. 建立目录 src存放源文件 classes存放编译文件 2. 建立类文件 主类 package test.ant; import test.ant.MyTool ...
- openstack搭建之-nova配置(10)
一. base节点设置数据库 mysql -u root -proot CREATE DATABASE nova_api; CREATE DATABASE nova; CREATE DATABASE ...
- #Leetcode# 997. Find the Town Judge
https://leetcode.com/problems/find-the-town-judge/ In a town, there are N people labelled from 1 to ...
- Python中的可视化神器:pyecharts
pyecharts是一款将python与echarts结合的强大的数据可视化工具,本文将为你阐述pyecharts的使用细则 前言 我们都知道python上的一款可视化工具matplotlib,而前些 ...
- Linux shell if判断语句
无论什么编程语言都离不开条件判断.SHELL也不例外. 大体的格式如下: if list then do something here elif list then do another thing ...