535. Encode and Decode TinyURL

LeetCode

link

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Codec {
Map<Integer,String> map=new HashMap<>();
String host="http://tinyurl.com/";
// Encodes a URL to a shortened URL.
public String encode(String longUrl) {
int key=longUrl.hashCode();
map.put(key,longUrl);
return host+key;
}
// Decodes a shortened URL to its original URL.
public String decode(String shortUrl) {
return map.get(Integer.valueOf(shortUrl.replace(host,"")));
}
}
// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.decode(codec.encode(url));

0%