import com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException;
import com.sun.org.apache.xml.internal.security.utils.Base64;
import java.util.HashMap;
import java.util.Map;
/**
* @author xnl
* @Description:
* @date: 2022/6/29 21:55
*/
public class Solution {
public static void main(String[] args) {
Solution solution = new Solution();
}
}
class Codec {
int id = 0;
Map<Integer, String> map = new HashMap<>();
// Encodes a URL to a shortened URL.
/**
* 使用hash表记录每一个请求,使用id区分请求
*
* @param longUrl
* @return
*/
public String encode(String longUrl) {
id++;
map.put(id, longUrl);
return "http://tinyurl.com/" + id;
}
// Decodes a shortened URL to its original URL.
/**
* 取出id,然后去hash表里面寻找对应的答案
* @param shortUrl
* @return
*/
public String decode(String shortUrl) {
int p = shortUrl.lastIndexOf("/") + 1;
int k = Integer.parseInt(shortUrl.substring(p));
return map.get(k);
}
}