Hi, this is Shunchi!

  • Home

  • Tags0

  • Archives267

  • Categories0

  • Curricula

  • DSA

  • LeetCode_Notes

  • Interviews

  • General

  • Resume

535. Encode and Decode TinyURL

Posted on 2020-08-05 | Edited on 2021-01-22

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));

<1…333435…267>
ShunchiZhou

ShunchiZhou

267 posts
RSS
GitHub E-Mail Gitbook Linkedin
© 2024 ShunchiZhou
Powered by Hexo v5.4.0
|
0%