0535.cpp (393B)
1 class Solution { 2 unordered_map<string, int> um; 3 vector<string> str; 4 5 public: 6 string encode(const string &longUrl) { 7 if (um.count(longUrl)) return to_string(um[longUrl]); 8 9 um.insert({longUrl, str.size()}); 10 str.push_back(longUrl); 11 return to_string(str.size() - 1); 12 } 13 14 string decode(const string &shortUrl) { return str[stoi(shortUrl)]; } 15 };