Monday, July 26, 2021

LintCode 1186 · Encode and Decode TinyURL.java

public class Solution {
List<String> list = new ArrayList<>();
public String encode(String longUrl) {
// Encodes a URL to a shortened URL.
list.add(longUrl);
return String.valueOf(list.size() - 1);
}
public String decode(String shortUrl) {
// Decodes a shortened URL to its original URL.
int id = Integer.valueOf(shortUrl);
return list.get(id);
}
}
// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.decode(codec.encode(url));

No comments:

Post a Comment