• 271. Encode and Decode Strings


    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.

    intuition: use a non-ASCII character as a delimiter to join the strings, such as Character.toString((char)257)

    encode: time = O(n), space = O(1)

    decode: time = O(n), space = O(n)

    public class Codec {
    
        // Encodes a list of strings to a single string.
        public String encode(List<String> strs) {
            if(strs.size() == 0) {
                return Character.toString((char)258);
            }
            
            String d = Character.toString((char)257);
            StringBuilder sb = new StringBuilder();
            for(String str : strs) {
                sb.append(str);
                sb.append(d);
            }
            sb.deleteCharAt(sb.length() - 1);
            return sb.toString();
        }
    
        // Decodes a single string to a list of strings.
        public List<String> decode(String s) {
            String d = Character.toString((char)258);
            if(s.equals(d)) {
                return new ArrayList<>();   // empty string
            }
            
            d = Character.toString((char)257);
            // split(String regex, int limit), limit controls the number of times the pattern is applied and therefore affects the length of the resulting array. 
            // If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter. 
            // If n is non-positive then the pattern will be applied as many times as possible and the array can have any length. 
            // If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.
            return Arrays.asList(s.split(d, -1));
        }
    }
    
    // Your Codec object will be instantiated and called as such:
    // Codec codec = new Codec();
    // codec.decode(codec.encode(strs));
  • 相关阅读:
    【SW4STM32生成 hex文件的设置方法】
    【004:结构体内存地址的连续问题】
    【003:使用SW4STM32不进入中断的原因】
    [转载来之雨松:NGUI研究院之为什么打开界面太慢(十三)]
    【错误总结1:unity StartCoroutine 报 NullReferenceException 错误】
    【场景切换的多种调用方式】
    【Lua学习笔记之:Lua环境搭建 Windows 不用 visual studio】
    【KeyCode 键码】
    mysql
    numpy
  • 原文地址:https://www.cnblogs.com/fatttcat/p/11364362.html
Copyright © 2020-2023  润新知