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. }

Machine 2 (receiver) has the function:

  1. vector<string> decode(string s) {
  2. //... your code
  3. return strs;
  4. }

So Machine 1 does:

  1. string encoded_string = encode(strs);

and Machine 2 does:

  1. 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.

思路:每个字符串之前添加它的长度和一个%字符。

  1. class Codec {
  2. public:
  3.  
  4. // Encodes a list of strings to a single string.
  5. string encode(vector<string>& strs) {
  6. string res;
  7. for (int i = ; i < strs.size(); i++)
  8. res += std::to_string(strs[i].size()) + "%" + strs[i];
  9. return res;
  10. }
  11.  
  12. // Decodes a single string to a list of strings.
  13. vector<string> decode(string s) {
  14. vector<string> res;
  15. int curInd = ;
  16. int mark = s.find("%");
  17. while (mark != std::string::npos) {
  18. int len = std::stoi(s.substr(curInd, mark - curInd));
  19. if (len) res.push_back(s.substr(mark + , len));
  20. //in case it is an empty string
  21. else res.push_back("");
  22. curInd = mark + len + ;
  23. mark = s.find("%", curInd);
  24. }
  25. return res;
  26. }
  27. };
  28.  
  29. // Your Codec object will be instantiated and called as such:
  30. // Codec codec;
  31. // codec.decode(codec.encode(strs));

Encode and Decode Strings -- LeetCode的更多相关文章

  1. [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 ...

  2. LeetCode Encode and Decode Strings

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

  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#271] Encode and Decode Strings

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

  5. 271. Encode and Decode Strings

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

  6. [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 ...

  7. Encode and Decode Strings

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

  8. Encode and Decode Strings 解答

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

  9. 535. Encode and Decode TinyURL - LeetCode

    Question 535. Encode and Decode TinyURL Solution 题目大意:实现长链接加密成短链接,短链接解密成长链接 思路:加密成短链接+key,将长链接按key保存 ...

随机推荐

  1. CF451E Devu and Flowers 解题报告

    CF451E Devu and Flowers 题意: \(Devu\)有\(N\)个盒子,第\(i\)个盒子中有\(c_i\)枝花.同一个盒子内的花颜色相同,不同盒子的花颜色不同.\(Devu\)要 ...

  2. Angular 监听路由变化

    var app = angular.module('Mywind',['ui.router']) //Angular 监听路由变化 function run($ionicPlatform, $loca ...

  3. 微信小程序使用Socket

    首先,一个小程序同时只能有一个WebSocket连接,如果当前已经存在一个WebSocket连接,会关闭当前连接,并重新建立一个连接. 其次,如果使用了appID,协议必须是 wss://... 最近 ...

  4. codevs 1191 线段树 区间更新(水)

    题目描述 Description 在一条数轴上有N个点,分别是1-N.一开始所有的点都被染成黑色.接着我们进行M次操作,第i次操作将[Li,Ri]这些点染成白色.请输出每个操作执行后剩余黑色点的个数. ...

  5. [POJ2777] Count Color

    \[Count Color\] Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 50865 Accepted: 15346 Des ...

  6. iebackground+icon图标兼容

    <!DOCTYPE > <html> <head> <title>zepto</title> <meta name="nam ...

  7. NOIP2005过河(青蛙过河)

    题目传送门 这道题主要是因为L长度最大可以为1e9 而石子却最多只有100个 这样就浪费了很多时间空间 所以我们压缩一波路径就可以了 剩余的就是枚举每个点以及i-y到i-x的dp了 这里要说一句为什么 ...

  8. SQLSERVER数据库置疑、可疑、脱机、单用户、紧急模式等的修复

    数据库出现置疑.可疑.脱机.单用户.紧急模式主要是因为数据库的日志文件除了问题,2000和2008修复方式不一样,2008的修复脚本在2000中不适用,主要是不被2000识别. 假设数据库名为:eis ...

  9. 【“10”力全开 游戏“Ti”厉害】ZX53VE-新飞行堡垒笔记本(Windows 10 Home/新七代标压i7-7700HQ/GTX 1050Ti 4G/8G内存/1TB+128GB)

    [“10”力全开 游戏“Ti”厉害]ZX53VE-新飞行堡垒笔记本(Windows 10 Home/新七代标压i7-7700HQ/GTX 1050Ti 4G/8G内存/1TB+128GB) http: ...

  10. [ Openstack ] OpenStack-Mitaka 高可用之 镜像服务(glance)

    目录 Openstack-Mitaka 高可用之 概述    Openstack-Mitaka 高可用之 环境初始化    Openstack-Mitaka 高可用之 Mariadb-Galera集群 ...