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. [Java123] JBoss

    https://blog.csdn.net/taogebx/article/details/4620760

  2. 利用 Settings Sync 同步vs code配置

    vs code上有各种各样不同的插件,如果要在不同的电脑上使用 vs code 配置是件比较麻烦的事情,使用 Settings Sync 将 vs code 配置备份起来,当需要在其他电脑使用  vs ...

  3. 高斯-克吕格投影与UTM投影

    高斯-克吕格投影与UTM投影 高斯-克吕格(Gauss-Kruger)投影与UTM投影(Universal Transverse Mercator,通用横轴墨卡托投影)都是横轴墨卡托投影的变种,目前一 ...

  4. 注冊成为Windows Phone开发人员而且解锁Windows Phone 8.1手机

    注冊成为Windows Phone开发人员而且解锁Windows Phone 8.1手机 上篇文章介绍了怎样使用Qt Creator和Visual Studio构建Windows Phone 8.1应 ...

  5. UWP 下载文件显示下载进度

    <Page x:Class="WgscdProject.TestDownloadPage" xmlns="http://schemas.microsoft.com/ ...

  6. 20155232《网络对抗》Exp 6 信息搜集与漏洞扫描

    20155232<网络对抗>Exp 6 信息搜集与漏洞扫描 一.实践内容 (1)各种搜索技巧的应用 (2)DNS IP注册信息的查询 (3)基本的扫描技术:主机发现.端口扫描.OS及服务版 ...

  7. python 回溯法 子集树模板 系列 —— 14、最长公共子序列(LCS)

    问题 输入 第1行:字符串A 第2行:字符串B (A,B的长度 <= 1000) 输出 输出最长的子序列,如果有多个,随意输出1个. 输入示例 belong cnblogs 输出示例 blog ...

  8. python 网络爬虫介绍

    一.网络爬虫相关概念 网络爬虫介绍 我们都知道,当前我们所处的时代是大数据的时代,在大数据时代,要进行数据分析,首先要有数据源,而学习爬虫,可以让我们获取更多的数据源,并且这些数据源可以按我们的目的进 ...

  9. mysql基础(一)——表、索引、视图

    SQL语句不区分大小写 创建数据库 create database myData 删除数据库 drop database myData 创建表 create table company ( code ...

  10. Redis简介、安装、配置、启用学习笔记

    前一篇文章有介绍关系型数据库和非关系型数据库的差异,现在就来学习一下用的较广的非关系型数据库:Redis数据库 Redis 简介 Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-v ...