class Solution {
public:
vector<string> res;
// Encodes a URL to a shortened URL.
string encode(string longUrl) {
res.push_back(longUrl);
return "http://tinyurl.com/"+to_string(res.size()-1);
}
// Decodes a shortened URL to its original URL.
string decode(string shortUrl) {
int index=shortUrl.find_last_of('/');
string temp=shortUrl.substr(index+1);
return res[stoi(temp)]; //stoi--string to int
}
};535. Encode and Decode TinyURL
最新推荐文章于 2025-05-23 02:45:00 发布
本文介绍了一种简单的短链接编码和解码方法。通过将原始URL存储在数组中,并返回其索引作为短链接的一部分,实现了URL的缩短。同时,也提供了根据短链接还原原始URL的功能。
189

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



