Question:

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.

Tips:

将长的url转换为短url 将短url转换为长的url。长url与短url之间有一个映射,保证一致。
如输入https://leetcode.com/problems/design-tinyurl 编码之后返回http://tinyurl.com/4e9iAk
输入http://tinyurl.com/4e9iAk,decode之后也要返回https://leetcode.com/problems/design-tinyurl。

编码方式没有限制,主要是code与decode之后 结果要相同。

思路:

code:

输入类似:https://leetcode.com/problems/design-tinyurl

用longUrl的哈希值作为hashmap的key,longUrl作为value,组成键值对,保存在map中。

返回"http://tinyurl.com/"+longUrl的哈希值,即键值。

decode:

输入类似:http://tinyurl.com/4e9iAk

根据http://tinyurl.com/ 后面的4e9iAk作为key 来查找对应的value就是longUrl

代码:(使用hashmap)

package medium;

import java.util.HashMap;
import java.util.Map; public class L535EncodeAndDecodeTinyURL {
//即将长的url转换为短url 将短url转换为长的url。
//长url与短url之间有一个映射,保证一致。
//如输入https://leetcode.com/problems/design-tinyurl 编码之后返回http://tinyurl.com/4e9iAk
//输入http://tinyurl.com/4e9iAk,decode之后也要返回https://leetcode.com/problems/design-tinyurl
Map<Integer, String> map = new HashMap<>(); // Encodes a URL to a shortened URL.
public String encode(String longUrl) {
map.put(longUrl.hashCode(), longUrl);
//System.out.println("long hashCode"+longUrl.hashCode());
return "http://tinyurl.com/" + longUrl.hashCode();
} // Decodes a shortened URL to its original URL.
public String decode(String shortUrl) {
return map.get(Integer.parseInt(shortUrl.replace("http://tinyurl.com/", "")));
} // Your Codec object will be instantiated and called as such:
public static void main(String[] args) {
L535EncodeAndDecodeTinyURL l535 = new L535EncodeAndDecodeTinyURL();
System.out.println(l535.decode(l535.encode("https://leetcode.com/problems/design-tinyurl")));
}
}

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

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

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

  2. 535. Encode and Decode TinyURL - LeetCode

    Question 535. Encode and Decode TinyURL Solution 题目大意:实现长链接加密成短链接,短链接解密成长链接 思路:加密成短链接+key,将长链接按key保存 ...

  3. LC 535. Encode and Decode TinyURL

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

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

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

  5. 535. Encode and Decode TinyURL 长短URL

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

  6. 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 ...

  7. 535. Encode and Decode TinyURL

    ▶ 要求给出一种对 URL 网址进行压缩和解压的算法,例如 https://leetcode.com/problems/design-tinyurl ←→ http://tinyurl.com/4e9 ...

  8. 535 Encode and Decode TinyURL 编码和解码精简URL地址

    详见:https://leetcode.com/problems/encode-and-decode-tinyurl/description/ C++: class Solution { public ...

  9. 【LeetCode】91. Decode Ways 解题报告(Python)

    [LeetCode]91. Decode Ways 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...

随机推荐

  1. Docker学习要点记录

    Docker的架构和底层技术: 1.docker提供了一个开发,打包,运行app的平台2.把APP和底层infrastructure隔离开来 docker底层技术支持: 1>Namespaces ...

  2. JS省市区联动效果

    省市区联动下拉效果在WEB中应用非常广泛,尤其在电商网站最为常见.一般使用Ajax实现无刷新下拉联动.利用jQuery,通过读取JSON数据,实现无刷新动态下拉省市二(三)级联动效果. 首先我们可以看 ...

  3. win10家庭版安装DockerToolbox-18.03.0-ce

    下载DockerToolbox-18.03.0-ce.exe https://mirrors.aliyun.com/docker-toolbox/windows/docker-toolbox/ 点击安 ...

  4. Leetcode——413. 等差数列划分

    题目描绘:题目链接 题目中需要求解一个数组中等差数组的个数,这个问题可以利用动态规划的思路来分析. 三步骤: 1:问题归纳.题目需要求解等差数列的和,我们可以用一个数组保存前i个元素可以构成的等差数列 ...

  5. 《MySQL:菜鸟入门系列》

    关于数据库相关知识,几乎是互联网从业者逃不开的一个必备技能,特别是对于DB.开发和测试童鞋来说,更显得重要... 关于MySQL,推荐如下几本书: 入门级:<MySQL必知必会> 进阶级: ...

  6. 【TPM】tpm搭建基础指南

    pm君第一次通过各种摸索,成功搭建了tpm模拟环境,本篇博客记录了如何去搭建tpm模拟环境,希望能给大家一些帮助. 参考资料(推荐看) ubuntu下安装TPM模拟器 --成功步骤:参考至第2节-安装 ...

  7. Hadoop开发第3期---Hadoop的伪分布式安装

    一.准备工作 1. 远程连接工具的安装 PieTTY 是在PuTTY 基础上开发的,改进了Putty 的用户界面,提供了多语种支持.Putty 作为远程连接linux 的工具,支持SSH 和telne ...

  8. task1

    centos定时任务 清空特定目录文件 https://www.jb51.net/article/151066.htm 这次linux下不生成日志文件主要是因为日志框架冲突问题,我解决问题的思路错了 ...

  9. idea 项目java版本选项位置

    藏这里了 还有一个

  10. Spring MVC统一异常处理

    实际上Spring MVC处理异常有3种方式: (1)一种是在Controller类内部使用@ExceptionHandler使用注解实现异常处理: 可以在Controller内部实现更个性化点异常处 ...