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));
  • 相关阅读:
    hadoop2.x整合手册【3】---编译sqoop的jar以及client的api调用
    hadoop2.x整合手册【2】---sqoop1.9.x安装与部署
    hadoop2.x整合手册【1】--hadoop2.x安装与配置
    "Mac OS X"录屏幕视频并转成gif
    osx launchpad图标的删除
    App Store中的开源游戏汇总
    App Store上的开源应用汇总
    转载:使用Auto Layout中的VFL(Visual format language)--代码实现自动布局
    UIViewController中各方法调用顺序及功能详解
    ios项目icon和default图片命名规则
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/5072662.html
Copyright © 2011-2022 走看看