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.

题目含义:设计一个短网址的编码和解码系统

 1 public class Codec {
 2     Map<Integer, String> map = new HashMap<>();
 3     
 4     // Encodes a URL to a shortened URL.
 5     public String encode(String longUrl) {
 6         map.put(longUrl.hashCode(),longUrl);
 7         return "http://tinyurl.com/"+longUrl.hashCode();        
 8     }
 9 
10     // Decodes a shortened URL to its original URL.
11     public String decode(String shortUrl) {
12         return map.get(Integer.parseInt(shortUrl.replace("http://tinyurl.com/", "")));        
13     }
14 }

 

版权声明:本文为wzj4858原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/wzj4858/p/7719137.html