LeetCode[21] - Encode and Decode Strings

不難,但是要考慮好如何handle ""。
因?yàn)槠綍r(shí)都把“” 當(dāng)做Null對待,這里就犯渾了。
這題,要把Null特別mark一下為‘NULL’,而特別處理 “” empty string.

/*
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 code
  return encoded_string;
}
Machine 2 (receiver) has the function:
vector<string> decode(string s) {
  //... your code
  return strs;
}
So Machine 1 does:

string encoded_string = encode(strs);
and Machine 2 does:

vector<string> strs2 = decode(encoded_string);
strs2 in Machine 2 should be the same as strs in Machine 1.

Implement the encode and decode methods.

Note:
The string may contain any possible characters out of 256 valid ascii characters. Your algorithm should be generalized enough to work on any possible characters.
Do not use class member/global/static variables to store states. Your encode and decode algorithms should be stateless.
Do not rely on any library method such as eval or serialize methods. You should implement your own encode/decode algorithm.


Tags: String
Similar Problems: (E) Count and Say, (M) Serialize and Deserialize Binary Tree

*/

/*
Thoughts:
Break into integers
Use some special words to: 1. break line. 2. record null condition.
Note: "" empty string is also a string case, so don't treat that as null. Call null, "NULL"
Note2: As long as the list is not empty, though some string might be just "", make sure to encode it as 'LINE' just to remind in decoder: treat it as a ""
*/
public class Codec {
     // Encodes a list of strings to a single string.
    public static String encode(List<String> strs) {
        if (strs == null || strs.size() == 0) {
            return "NULL";
        }
        StringBuffer sb = new StringBuffer();
        for (String str : strs) {
            char[] arr = str.toCharArray();
            for (int i = 0; i < arr.length; i++) {
                if (arr[i] >= 100) {
                    sb.append("" + (int)arr[i]);
                } else if (arr[i] >= 10) {
                    sb.append("0" + (int)arr[i]);
                } else {
                    sb.append("00" + (int)arr[i]);
                }
            }
            sb.append("LINE");
        }//END for
        if (sb.length() == 0) {
            sb.append("LINE");
        }
        return sb.toString();
    }

    // Decodes a single string to a list of strings.
    public static List<String> decode(String s) {
        List<String> rst = new ArrayList<String>();
        if (s.equals("NULL")) {
            return rst;
        }
        int index = s.indexOf("LINE");
        while (index != -1) {
            String str = s.substring(0, index);

            StringBuffer sb = new StringBuffer();
            int i = 0;
            while (i + 3 <= str.length()) {
                int letter = Integer.parseInt(str.substring(i, i + 3));
                sb.append((char)letter);
                i+=3;
            }
            rst.add(sb.toString());

            s = s.substring(index + 4);
            index = s.indexOf("LINE");
        }
        
        return rst;
    }
}
// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.decode(codec.encode(strs));

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容