程式語言 - LeetCode - C++ - 535. Encode and Decode TinyURL



題目:


解答:

class Solution {
private:
    vector<string> url;

public:

    // Encodes a URL to a shortened URL.
    string encode(string longUrl) {
        url.push_back(longUrl);

        int id = url.size() - 1;
        return "http://tinyurl.com/" + to_string(id);
    }

    // Decodes a shortened URL to its original URL.
    string decode(string shortUrl) {
        int pos = shortUrl.find_last_of('/');
        int id = stoi(shortUrl.substr(pos + 1));

        return url[id];
    }
};

// Your Solution object will be instantiated and called as such:
// Solution solution;
// solution.decode(solution.encode(url));