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. android camera 摄像头预览画面变形

    问题:最近在处理一下camera的问题,发现在竖屏时预览图像会变形,而横屏时正常.但有的手机则是横竖屏都会变形. 结果:解决了预览变形的问题,同时支持前后摄像头,预览无变形,拍照生成的jpg照片方向正 ...

  2. MySQL(六)创建用户与授权

    转载自:MySQL创建用户与授权 目录 一.创建用户 二.授权 三.设置和更改用户密码 四.撤销用户权限 五.删除用户 一.创建用户 命令: CREATE USER 'username'@'host' ...

  3. BT5R3蛋疼的metasploit升级

    刚装了BT5R3,急着想把metasploit升级,原版本是4.5.0,试了网上的各种方法,终于试到了个能成功的,再次记录一下. 系统环境:BT5 R3 1.apt-get update 2.apt- ...

  4. jquery mouseover与mouseenter区别

    <!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...

  5. python之Django实现商城从0到1

    dailyfresh-B2Cdailyfresh mall based on B2C model 基于B2C的天天生鲜商城 项目托管地址:https://github.com/Ylisen/daily ...

  6. Django Rest Framework源码剖析(五)-----解析器

    一.简介 解析器顾名思义就是对请求体进行解析.为什么要有解析器?原因很简单,当后台和前端进行交互的时候数据类型不一定都是表单数据或者json,当然也有其他类型的数据格式,比如xml,所以需要解析这类数 ...

  7. flask前端与后端之间传递的两种数据格式:json与FormData

    json格式 双向! 前端 ==>后端:json格式 后端 ==>前端:json格式 html <!-- html部分 --> <form enctype='applic ...

  8. Eclipse中Hadoop插件配置

    Eclipse中Hadoop插件DFS配置 http://www.cnblogs.com/xia520pi/archive/2012/05/20/2510723.html

  9. 5.Xilinx RapidIO核例子工程源码分析

    https://www.cnblogs.com/liujinggang/p/10091216.html 一.软件平台与硬件平台 软件平台: 操作系统:Windows 8.1 64-bit 开发套件:V ...

  10. pycharm如何在虚拟环境中引入别人的项目

    如果你想引入别人的项目,但是呢引入的项目可能与自己原先装的模块的版本产生冲突,而且如果引入一个项目就在本地进行运行使用,每个项目用的依赖包都不大相同,就会导致解释器安装包过多,就会导致加载过慢,甚至会 ...