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));
  • 相关阅读:
    Java Web(5) Spring 下使用Junit4 单元测试
    聊聊单元测试(三)——Spring Test+JUnit完美组合
    浅谈ELK日志分析平台
    ELK 实现 Java 分布式系统日志分析架构
    ELK(ElasticSearch, Logstash, Kibana)搭建实时日志分析平台
    开源分布式搜索平台ELK(Elasticsearch+Logstash+Kibana)入门学习资源索引
    自动补全下拉框(可输入匹配的下拉框)
    这是一篇满载真诚的微信小程序开发干货
    微服务化的多组件项目,跨地域、分布式版本管理和发布方式
    解放双手,发掘更大的价值:智能化运维
  • 原文地址:https://www.cnblogs.com/fatttcat/p/11364362.html
Copyright © 2011-2022 走看看