[LeetCode] Encode and Decode Strings

本文介绍了一种用于将字符串列表编码为单个字符串的方法,并通过网络传输,随后在接收端解码回原始字符串列表。编码算法使用特定字符作为分隔符,同时考虑输入字符串可能包含的所有ASCII字符。解码过程从找到第一个分隔符开始,计算其后的字符串长度,以此提取原始字符串。通过示例展示编码与解码的完整流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem Description:

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.

Well, let's use an example strs = ["#$%", "", "12"] to illustrate the encoding and decoding algorithms. The idea is to use some character as sentinel. But since the input strs may contain any character, including the sentinel, we still need to use other information to avoid ambiguation. Specifically, we use the length of the string. The above string will be encoded as

3##$%0#2#12

Each color represents the encoding of each string and we use # as the sentinel.

During decoding, we will initialize a starting point p to be 0. Then we find the first # starting from p, which is just the sentinel for the first string and characters between p and the first # encode the length of the following string, using which we would be able to extract that string. In the above example, the length of the first string is 3 and we extract 3 characters after the first sentinel # and get #$%, which is just the first string. Then we move p to the point after the first string and continue the above process. Finally, all strings will be extracted out.

The code is as follows. If you find it not that clear, run it on the above example and you will get how it works.

 1 class Codec {
 2 public:
 3 
 4     // Encodes a list of strings to a single string.
 5     string encode(vector<string>& strs) {
 6         string s;
 7         for (string str : strs)
 8             s += to_string(str.length()) + '$' + str;
 9         return s;
10     }
11 
12     // Decodes a single string to a list of strings.
13     vector<string> decode(string s) {
14         vector<string> strs;
15         size_t n = s.length(), p = 0 ;
16         while (p < n) {
17             size_t pos = s.find('$', p);
18             if (pos == string::npos) break;
19             size_t sz = stoi(s.substr(p, pos - p));
20             strs.push_back(s.substr(pos + 1, sz));
21             p = pos + sz + 1;
22         }
23         return strs;
24     }
25 };
26 
27 // Your Codec object will be instantiated and called as such:
28 // Codec codec;
29 // codec.decode(codec.encode(strs));

BTW, string::npos means the end of a string: if we reach the end of a string, that means there is no sentinel and all the strings have been extracted out, so we will return.

转载于:https://www.cnblogs.com/jcliBlogger/p/4768875.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值