zoukankan      html  css  js  c++  java
  • 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));
  • 相关阅读:
    洛谷 P2048 [NOI2010]超级钢琴(优先队列,RMQ)
    洛谷P1074 靶形数独(跳舞链)
    洛谷P1337 [JSOI2004]平衡点 / 吊打XXX(模拟退火)
    洛谷P4003 无限之环(费用流)
    洛谷P3264 [JLOI2015]管道连接(斯坦纳树)
    洛谷P3190 [HNOI2007]神奇游乐园(插头dp)
    洛谷P3272 [SCOI2011]地板(插头dp)
    常用的端口配置
    Win7+Eclipse+Hadoop2.6.4开发环境搭建
    Windows10+eclipse+hadoop2.7.1环境配置+wordcount-折腾笔记
  • 原文地址:https://www.cnblogs.com/fatttcat/p/11364362.html
Copyright © 2011-2022 走看看