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. P4279 [SHOI2008]小约翰的游戏

    嘟嘟嘟 一道博弈论经典题,nim游戏. 只不过要考虑有奇数个石子为1的堆的时候,为Brother赢.剩下就是nim游戏了. 极简代码 #include<cstdio> using name ...

  2. 1 TCP/IP通信

    重点参考长链接http://blog.csdn.net/fengyuzhengfan/article/details/38830115 http://blog.csdn.net/Jsagacity/a ...

  3. MyBatis实战之映射器

    映射器是MyBatis最强大的工具,也是我们使用MyBatis时用得最多的工具,因此熟练掌握它十分必要.MyBatis是针对映射器构造的SQL构建的轻量级框架,并且通过配置生成对应的JavaBean返 ...

  4. Android关于LinearLayout和RelativeLayout背景设置的区别

    1.LinearLayout:设置背景时即设置android:background时,假如LayoutLayout设置了android:layout_width="wrap_content& ...

  5. js 自己项目中几种打开或弹出页面的方法

    自己项目中,几种打开或弹出页面的方法(部分需要特定环境下) var blnTop = false;//是否在顶层显示 ///动态生成模态窗体(通过字符串生成) ///strModalId:模态窗体ID ...

  6. Swift10大开源项目记录

    Alamofire : Swift编写的HTTP网络库,用于异步网络通信. Surge: Surge基于Accelerate框架开发,用于执行矩阵数学.数字信号处理以及图像处理等方面. SwiftyJ ...

  7. pdflush机制

    在做进程安全监控的时候,拍脑袋决定的,如果发现一个进程在D状态时,即TASK_UNINTERRUPTIBLE(不可中断的睡眠状态),时间超过了8min,就将系统panic掉.恰好DB组做日志时,将整个 ...

  8. Delphi DBGrid类控件定位到某一行,并更改为选中状态。

    Delphi中,可以使用数据集控件提供的 Locate 成员方法快速定位至某条记录, 然后通过清除数据集控件的选中状态,并重新赋值达到我们的目的. grDirectory.DataSource.Dat ...

  9. face_recognition环境配置及命令行工具测试

    由于某种不可抗力(又是它!)我写了这篇博客,主要目的是记录. face_recognition是啥子? face_recognition号称世界上最简单的人脸识别库,可使用 Python 和命令行进行 ...

  10. java 锁白话

    一.锁 1.可见性: 定义:数据对所有线程可见 原因:cpu操作数据时会把数据读取到内存中去,可以理解为值做了备份,但是备份数据和原始数据在后续操作中不一定一致 实现:java使用volite关键字来 ...