zoukankan      html  css  js  c++  java
  • [LeetCode] 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.

    字符串的编码与解码。

    请你设计一个算法,可以将一个 字符串列表 编码成为一个 字符串。这个编码后的字符串是可以通过网络进行高效传送的,并且可以在接收端被解码回原来的字符串列表。

    思路如下,编码的时候,可以编成这样的格式:stringLength + "/" + string

    这样解码的时候,可以通过找到第一个"/"的方式确定每个string的长度,也可以通过这个"/"的位置确定每个string的起始位置,哪怕string本身有"/"。

    时间O(n)

    空间O(n)

    Java实现

     1 public class Codec {
     2 
     3     // Encodes a list of strings to a single string.
     4     public String encode(List<String> strs) {
     5         StringBuilder sb = new StringBuilder();
     6         for (String str : strs) {
     7             // length + '/' + string
     8             sb.append(str.length()).append('/').append(str);
     9         }
    10         return sb.toString();
    11     }
    12 
    13     // Decodes a single string to a list of strings.
    14     public List<String> decode(String s) {
    15         List<String> res = new ArrayList<>();
    16         int i = 0;
    17         while (i < s.length()) {
    18             int slash = s.indexOf('/', i);
    19             int size = Integer.valueOf(s.substring(i, slash));
    20             res.add(s.substring(slash + 1, slash + 1 + size));
    21             i = slash + 1 + size;
    22         }
    23         return res;
    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));

    JavaScript实现

     1 /**
     2  * Encodes a list of strings to a single string.
     3  *
     4  * @param {string[]} strs
     5  * @return {string}
     6  */
     7 var encode = function (strs) {
     8     let sb = [];
     9     for (let str of strs) {
    10         sb.push(str.length);
    11         sb.push('/');
    12         sb.push(str);
    13     }
    14     return sb.join('');
    15 };
    16 
    17 /**
    18  * Decodes a single string to a list of strings.
    19  *
    20  * @param {string} s
    21  * @return {string[]}
    22  */
    23 var decode = function (s) {
    24     let res = [];
    25     let i = 0;
    26     while (i < s.length) {
    27         let slash = s.indexOf('/', i);
    28         let len = parseInt(s.substring(i, slash));
    29         let str = s.substring(slash + 1, slash + 1 + len);
    30         res.push(str);
    31         i = slash + 1 + len;
    32     }
    33     return res;
    34 };
    35 
    36 /**
    37  * Your functions will be called as such:
    38  * decode(encode(strs));
    39  */

    相关题目

    38. Count and Say

    271. Encode and Decode Strings

    443. String Compression

    604. Design Compressed String Iterator

    1313. Decompress Run-Length Encoded List

    LeetCode 题目总结

  • 相关阅读:
    Hbase源码分析:Hbase UI中Requests Per Second的具体含义
    Hbase源码分析:server端RPC
    Hbase源码分析:RPC概况
    python使用mysql connection获取数据感知不到数据变化问题
    虚拟机加载机制读后感
    Spark学习笔记1:Application,Driver,Job,Task,Stage理解
    架构模式: 日志聚合
    架构模式: 服务集成协议测试
    架构模式: 服务组件测试
    架构模式: 访问令牌
  • 原文地址:https://www.cnblogs.com/cnoodle/p/13237545.html
Copyright © 2011-2022 走看看