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.

一刷
题解:
encode就是在每个单词之后加一个“/”
decode: string.split("/");

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 11,222评论 0 23
  • 原来你也在望着我,在我上一秒还望着你的时候。 爱还在远方,在我还什么都不懂的时候,潜意识就已经代替我的后知后觉,刻...
    南宫宸阅读 239评论 0 0
  • 来到你在的校园 原地转了好几个圈 看着路灯下面 他们的脸 笑容有点甜 不知你和她在哪 在哪把手儿牵 在哪把这园儿转...
    小呀小白杨阅读 143评论 0 0
  • 爱情 文/江南 一只猫的叫声, 让黑夜, 毛骨悚然。 我想,再也没有 一桩爱情 如此凄惨。 ...
    坝上客阅读 159评论 0 0

友情链接更多精彩内容