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:

  1. string encode(vector<string> strs) {
  2. // ... your code
  3. return encoded_string;
  4. }
  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. int len = str.length();
  8. sb.append(len).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 len = Integer.valueOf(s.substring(i, slash));
  20. res.add(s.substring(slash + 1, slash + 1 + len));
  21. i = slash + 1 + len;
  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));

[LC] 271. Encode and Decode Strings的更多相关文章

  1. 271. Encode and Decode Strings

    题目: Design an algorithm to encode a list of strings to a string. The encoded string is then sent ove ...

  2. [LeetCode#271] Encode and Decode Strings

    Problem: Design an algorithm to encode a list of strings to a string. The encoded string is then sen ...

  3. [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 th ...

  4. [LeetCode] Encode and Decode Strings 加码解码字符串

    Design an algorithm to encode a list of strings to a string. The encoded string is then sent over th ...

  5. LeetCode Encode and Decode Strings

    原题链接在这里:https://leetcode.com/problems/encode-and-decode-strings/ 题目: Design an algorithm to encode a ...

  6. Encode and Decode Strings

    Design an algorithm to encode a list of strings to a string. The encoded string is then sent over th ...

  7. Encode and Decode Strings 解答

    Question Design an algorithm to encode a list of strings to a string. The encoded string is then sen ...

  8. [Swift]LeetCode271. 加码解码字符串 $ Encode and Decode Strings

    Design an algorithm to encode a list of strings to a string. The encoded string is then sent over th ...

  9. Encode and Decode Strings -- LeetCode

    Design an algorithm to encode a list of strings to a string. The encoded string is then sent over th ...

随机推荐

  1. CSU2004:Finding words(含指定不相交前后缀的模式串计数)

    题:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=2004 题意:给定n个模式串,m个询问,每个询问是“前缀+‘*’+后缀 ”的组合的串S,输出 ...

  2. ACWING基础算法(三)

    双指针算法. 相向双指针,指的是在算法的一开始,两根指针分别位于数组/字符串的两端,并相向行走. ACWING 的一道裸题(不知道为啥进不去404):最长连续不重复子序列 输入 5 1 2 2 3 5 ...

  3. NRF24L01中断双向传输数据

    NRF24L01是一款比较常见的无线通讯芯片,不过有个缺点就是只能半双工通讯,当涉及到双向通讯时就比较麻烦一些·,特别是想要做无线IAP数据需要一直来回发送,这点无疑然人恶心到想吐,不过还好有数据中断 ...

  4. 吴裕雄--天生自然MySQL学习笔记:MySQL 插入数据

    MySQL 表中使用 INSERT INTO SQL语句来插入数据. 可以通过 mysql> 命令提示窗口中向数据表中插入数据,或者通过PHP脚本来插入数据. 以下为向MySQL数据表插入数据通 ...

  5. 1月18日 LCA专项训练

    A. Lorenzo Von Matterhorn B.Minimum spanning tree for each edge C.Misha, Grisha and Underground D.Fo ...

  6. 2019.11.18CTFD搭建记录

    ### 0x01 实验室纳新,准备在自己服务器搭建个ctfd给新生们玩玩,忙活了一天orz[大一刚开学就搭建过没这么费力啊..] 现在大二了没想到能折腾一天... 直接说下我踩的坑吧,给后来的人们说说 ...

  7. tar.xz文件

    创建或解压tar.xz文件的方法 习惯了 tar czvf 或 tar xzvf 的人可能碰到 tar.xz也会想用单一命令搞定解压或压缩.其实不行 tar里面没有征对xz格式的参数比如 z是针对 g ...

  8. windows 安装Bitcoin Core使用

    1.官网下载https://bitcoin.org/en/download 选择Windows  其他系统就选择对应的就好 2.双击安装完过后,进入bin目录,打开bitcoin-qt.exe运行,提 ...

  9. PAT Advanced 1127 ZigZagging on a Tree (30) [中序后序建树,层序遍历]

    题目 Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree c ...

  10. Java之多线程窗口卖票问题(Thread)

    /** * * 例子:创建三个窗口卖票,总票数为100张.使用继承Thread类的方式 * * 存在线程的安全问题,待解决. * */class Window extends Thread{ priv ...