LeetCode "Encode and Decode Strings"

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;
public:

    // Encodes a list of strings to a single string.
    string encode(vector<string>& strs) {
        string ret;
        for(auto &s : strs)
        {
            int i = 0, len = s.length();

            while(i < len)
            {
                UCHAR c = s[i];
                UCHAR cnt = 1;
                while(i < len - 1 && s[i + 1] == c && cnt < (MAX_CNT - 1))
                {
                    i ++; cnt ++;
                }
                ret += UCHAR(cnt);
                ret += UCHAR(c);

                i ++;
            }
            ret += UCHAR(MAX_CNT); // 0xFF: end
        }
        return ret;
    }

    // Decodes a single string to a list of strings.
    vector<string> decode(string s)
    {
        vector<string> ret;

        size_t len = s.length();
        string cur; int inx = 0;
        while(inx < len)
        {
            UCHAR cnt = s[inx];
            if(cnt == UCHAR(MAX_CNT))
            {
                ret.push_back(cur);
                cur = "";
                inx ++;
                continue;
            }
            //
            UCHAR c = s[inx + 1];
            for(UCHAR i = 0; i < cnt; i ++)    cur += c;
            inx += 2;
        }
        return ret;
    }
};
时间: 2024-08-14 17:49:28

LeetCode "Encode and Decode Strings"的相关文章

271. Encode and Decode Strings

/* * 271. Encode and Decode Strings * 2016-6-25 by Mingyang * 这道题很酷的地方就是选择一种存储方式可以有效地存储string,我一开始就想到了存长度加string的方法 * 这个题用了一个indexof的API, * public int indexOf(String str,int fromIndex) * Returns the index within this string of the first occurrence of

[LeetCode#271] Encode and Decode Strings

Problem: Design an algorithm to encode a list of strings to a string. The encoded string is then sent over the network and is decoded back to the original list of strings. Machine 1 (sender) has the function: string encode(vector<string> strs) { //

Encode and Decode Strings -- LeetCode

Design an algorithm to encode a list of strings to a string. The encoded string is then sent over the network and is decoded back to the original list of strings. Machine 1 (sender) has the function: string encode(vector<string> strs) { // ... your

[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

Encode and Decode Strings 解答

Question Design an algorithm to encode a list of strings to a string. The encoded string is then sent over the network and is decoded back to the original list of strings. Machine 1 (sender) has the function: string encode(vector<string> strs) { //

[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 res

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

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