zoukankan      html  css  js  c++  java
  • Leetcode: Encode and Decode Strings

     1 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.
     2 
     3 Machine 1 (sender) has the function:
     4 
     5 string encode(vector<string> strs) {
     6   // ... your code
     7   return encoded_string;
     8 }
     9 Machine 2 (receiver) has the function:
    10 vector<string> decode(string s) {
    11   //... your code
    12   return strs;
    13 }
    14 So Machine 1 does:
    15 
    16 string encoded_string = encode(strs);
    17 and Machine 2 does:
    18 
    19 vector<string> strs2 = decode(encoded_string);
    20 strs2 in Machine 2 should be the same as strs in Machine 1.
    21 
    22 Implement the encode and decode methods.
    23 
    24 Note:
    25 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.
    26 Do not use class member/global/static variables to store states. Your encode and decode algorithms should be stateless.
    27 Do not rely on any library method such as eval or serialize methods. You should implement your own encode/decode algorithm.

    If I choose / as spliter, how to ensure the other / wouldn't be seen as spliter? The idea is to store length of the str

    This one will be encoded as "6/aa2/bb". When decoding, it finds a string of length of 6 which includes "aa2/bb" so it won't be able to read the "2/" as you might be thinking of.

     1 public class Codec {
     2 
     3     // Encodes a list of strings to a single string.
     4     public String encode(List<String> strs) {
     5         StringBuffer res = new StringBuffer();
     6         for (int i=0; i<strs.size(); i++) {
     7             res.append(strs.get(i).length());
     8             res.append('#');
     9             res.append(strs.get(i));
    10         }
    11         return res.toString();
    12     }
    13 
    14     // Decodes a single string to a list of strings.
    15     public List<String> decode(String s) {
    16         List<String> result = new ArrayList<String>();
    17         while (s.length() > 0) {
    18             int i = s.indexOf("#");
    19             int count = Integer.parseInt(s.substring(0, i));
    20             result.add(s.substring(i+1, i+1+count));
    21             s = s.substring(i+1+count);
    22         }
    23         return result;
    24     }
    25 }
    26 
    27 // Your Codec object will be instantiated and called as such:
    28 // Codec codec = new Codec();
    29 // codec.decode(codec.encode(strs));
  • 相关阅读:
    SQL Server 附加数据库,报只读文件,无权修改其中某些文件
    NLog.config 配置
    系统架构设计师论文可靠性设计
    二、软件设计原则
    JavaScript 判断数组是否含有重复值
    mysql 添加索引 mysql 如何创建和删除索引
    利用pandas,BytesIO,zipfile打包csv文件,生成压缩文件
    不良人mysql索引
    转mysql数据库允许空值索引问题
    多线程中ThreadPoolExecutor.map()中传递多个参数
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/5072662.html
Copyright © 2011-2022 走看看