[LeetCode] Encode and Decode TinyURL

TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problems/design-tinyurl and it returns a short URL such as http://tinyurl.com/4e9iAk.

Design the encode and decode methods for the TinyURL service. There is no restriction on how your encode/decode algorithm should work. You just need to ensure that a URL can be encoded to a tiny URL and the tiny URL can be decoded to the original URL.

encode: 将longUrl压入栈中,并将longUrl后缀转换为数字加入到tinyurl的后缀。

decode: 找出tinyurl的后缀数字,返回栈中数字对应的longUrl.

class Solution {
public:
    vector<string> urls;
    // Encodes a URL to a shortened URL.
    string encode(string longUrl) {
        urls.push_back(longUrl);
        return "http://tinyurl.com/" + to_string(urls.size() - 1);
    }

    // Decodes a shortened URL to its original URL.
    string decode(string shortUrl) {
        auto pos = shortUrl.find_last_of(‘/‘);
        int num = stoi(shortUrl.substr(pos + 1));
        return urls[num];
    }
};
// 3ms

// Your Solution object will be instantiated and called as such:
// Solution solution;
// solution.decode(solution.encode(url));
时间: 2024-10-17 21:13:14

[LeetCode] Encode and Decode TinyURL的相关文章

[LeetCode] Encode and Decode TinyURL 编码和解码小URL地址

Note: This is a companion problem to the System Design problem: Design TinyURL. TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problems/design-tinyurl and it returns a short URL such as http://tinyurl.com/4e9iA

535. Encode and Decode TinyURL - LeetCode

Question 535. Encode and Decode TinyURL Solution 题目大意:实现长链接加密成短链接,短链接解密成长链接 思路:加密成短链接+key,将长链接按key保存到map,解密时根据短链接提取key,再从map中返回长链接 Java实现: public class Codec { // https://leetcode.com/problems/design-tinyurl --> http://tinyurl.com/4e9iAk Map<Integer

Encode and Decode TinyURL

Encode and Decode TinyURL TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problems/design-tinyurl and it returns a short URL such as http://tinyurl.com/4e9iAk. Design the encode and decode methods for the TinyUR

LeetCode 解题思路:535.Encode and Decode TinyURL

TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problems/design-tinyurl and it returns a short URL such as http://tinyurl.com/4e9iAk. Design the encode and decode methods for the TinyURL service. There is no res

[LeetCode] 535. Encode and Decode TinyURL 编码和解码短URL

Note: This is a companion problem to the System Design problem: Design TinyURL.TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problems/design-tinyurl and it returns a short URL such as http://tinyurl.com/4e9iAk

leetcode535 - Encode and Decode TinyURL - medium

TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problems/design-tinyurl and it returns a short URL such as http://tinyurl.com/4e9iAk.Design the encode and decode methods for the TinyURL service. There is no rest

535. Encode and Decode TinyURL(rand and srand)

Note: This is a companion problem to the System Design problem: Design TinyURL. TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problems/design-tinyurl and it returns a short URL such as http://tinyurl.com/4e9iA

LeetCode &quot;Encode and Decode Strings&quot;

This is about https://en.wikipedia.org/wiki/Run-length_encoding. The trick is, for a valid char, we only compress up to 254 occurences - count 255 means end of a string. typedef unsigned char UCHAR; class Codec { const static int MAX_CNT = 255; publi

535. Encode and Decode TinyURL

class Solution { public: long max_id = 0; unordered_map<long,string> id_long; // Encodes a URL to a shortened URL. string encode(string longUrl) { id_long[max_id++] = longUrl; return to_string(max_id - 1); } // Decodes a shortened URL to its origina