解法一
计数作为编码。
public class Codec {
private Map<Integer, String> map;
private int index;
public Codec() {
map = new HashMap<>();
index = 0;
}
// Encodes a URL to a shortened URL.
public String encode(String longUrl) {
map.put(index, longUrl);
return "http://tinyurl.com/" + index++;
}
// Decodes a shortened URL to its original URL.
public String decode(String shortUrl) {
return map.get(Integer.parseInt(shortUrl.substring(19, shortUrl.length())));
}
}
// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.decode(codec.encode(url));