Created
January 2, 2019 06:19
-
-
Save kanrourou/d4a1899c679385aeb26eccb8f398e324 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
// Encodes a URL to a shortened URL. | |
string encode(string longUrl) { | |
int base = 62, id = _id++; | |
string res = ""; | |
while(id) | |
{ | |
int digit = id % base; | |
res = _s[digit] + res; | |
id /= base; | |
} | |
if(res.size() < 6)res = string(6 - res.size(), '0') + res; | |
_s2l[res] = longUrl; | |
return res; | |
} | |
// Decodes a shortened URL to its original URL. | |
string decode(string shortUrl) { | |
return _s2l[shortUrl]; | |
} | |
private: | |
int _id = 0; | |
string _s = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
unordered_map<string, string> _s2l; | |
}; | |
// Your Solution object will be instantiated and called as such: | |
// Solution solution; | |
// solution.decode(solution.encode(url)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment