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);
}
}
力扣:535. TinyURL 的加密与解密
最新推荐文章于 2025-11-24 01:23:29 发布
该博客介绍了如何使用Java实现URL的编码和解码功能。`Codec`类包含两个方法,`encode`用于将长URL转化为短URL,通过一个HashMap存储ID与原始URL的映射;`decode`方法则根据短URL解析出原始URL。博客主要关注URL处理的基础操作。
186

被折叠的 条评论
为什么被折叠?



