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.

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

 class Codec {
public: // Encodes a list of strings to a single string.
string encode(vector<string>& strs) {
string res;
for (int i = ; i < strs.size(); i++)
res += std::to_string(strs[i].size()) + "%" + strs[i];
return res;
} // Decodes a single string to a list of strings.
vector<string> decode(string s) {
vector<string> res;
int curInd = ;
int mark = s.find("%");
while (mark != std::string::npos) {
int len = std::stoi(s.substr(curInd, mark - curInd));
if (len) res.push_back(s.substr(mark + , len));
//in case it is an empty string
else res.push_back("");
curInd = mark + len + ;
mark = s.find("%", curInd);
}
return res;
}
}; // Your Codec object will be instantiated and called as such:
// Codec codec;
// 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. git使用笔记(二)分支与合并

    By francis_hao    Nov 18,2016 查看分支,* 表示当前所在分支 $ git branch 查看分支和最后一次提交记录 $ git branch -v 新建分支 $ git ...

  2. C ------ 标准函数介绍

    sprintf() 函数原型:int sprintf( char *buffer, const char *format [, argument] ... ); 功能介绍: 1.把一个字符串赋值(拷贝 ...

  3. JavaScript学习笔记——浅拷贝、深拷贝

    参考自:http://www.cnblogs.com/yichengbo/archive/2014/07/10/3835882.html 一.数组的深浅拷贝 在使用JavaScript对数组进行操作的 ...

  4. css和javascript中图片路径的不同

    之前在写前端代码时,在图片路径的设置那里经常会遇到一个问题.比方说,我 (1)在根目录下面新建了个"images"文夹,里面放了张图片top.gif (2)在根目录下另外新建了两个 ...

  5. MDK stm32 仿真

    直接选择simulator,仿真时报错 *** error 65: access violation at 0x40021000 : no 'read' permission 修改系统配置,原配置如下 ...

  6. 应对ubuntu linux图形界面卡住的方法

    有的时候,我的ubuntu图形界面会卡住,当然这个时候你可以重新启动,不过最好的办法应该是结束这个桌面进程 那桌面卡住了怎么来结束桌面进程呢? 这时候就需要打开tty了 按下键盘ctrl+alt+f1 ...

  7. HDU1010(dfs+剪枝)

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  8. Jmeter4.0启动闪退问题解决方案

    jmeter:4.0 jdk版本:1.8 在Jmeter.bat的最后添加pause可以让Jmeter启动停止: 添加了pause进行强制停止在启动命令页面,查看到Jmeter报错信息如下: 第一次解 ...

  9. 【Android开发日记】之入门篇(十三)——Android的控件解析

    Android的控件都派生自android.view.View类,在android.widget包中定义了大量的系统控件供开发者使用,开发者也可以从View类及其子类中,派生出自定义的控件. 一.An ...

  10. TimeUnit 笔记

    TimeUnit笔记 1.TimeUnit 简介 TimeUnit是java.util.concurrent包下的一个枚举类,其主要封装了时间单位之间的转换以及基于时间上对线程的基本操作(sleep, ...