[LeetCode] 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.
这道题让我们给字符加码再解码,先有码再无码,然后题目中并没有限制加码的方法,那么只要能成功的把有码变成无码就行了,具体变换方法自己可以设计。由于需要把一个字符串集变成一个字符串,然后把这个字符串再还原成原来的字符串集,最开始博主想能不能在每一个字符串中间加个空格把它们连起来,然后再按空格来隔开,但是这种方法的问题是原来的一个字符串中如果含有空格,那么还原的时候就会被分隔成两个字符串,所以必须还要加上长度的信息,加码方法是长度 + "/" + 字符串,比如对于 "a","ab","abc",就变成 "1/a2/ab3/abc",那么解码的时候就有规律可寻,先寻找 "/",然后之前的就是要取出的字符个数,从 "/" 后取出相应个数即可,以此类推直至没有 "/"了,这样就得到高清无码的字符串集了,参见代码如下:
解法一:
class Codec {
public:
// Encodes a list of strings to a single string.
string encode(vector<string>& strs) {
string res = "";
for (auto a : strs) {
res.append(to_string(a.size())).append("/").append(a);
}
return res;
}
// Decodes a single string to a list of strings.
vector<string> decode(string s) {
vector<string> res;
int i = ;
while (i < s.size()) {
auto found = s.find("/", i);
int len = stoi(s.substr(i, found - i));
res.push_back(s.substr(found + , len));
i = found + len + ;
}
return res;
}
};
上面的方法是用一个变量i来记录当前遍历到的位置,我们也可以通过修改修改s,将已经解码的字符串删掉,最终s变为空的时候停止循环,参见代码如下:
解法二:
class Codec {
public:
// Encodes a list of strings to a single string.
string encode(vector<string>& strs) {
string res = "";
for (auto a : strs) {
res.append(to_string(a.size())).append("/").append(a);
}
return res;
}
// Decodes a single string to a list of strings.
vector<string> decode(string s) {
vector<string> res;
while (!s.empty()) {
int found = s.find("/");
int len = stoi(s.substr(, found));
s = s.substr(found + );
res.push_back(s.substr(, len));
s = s.substr(len);
}
return res;
}
};
我们还可以使用更简单的压缩方法,比如在每个字符串的后面加上换行字符 '\0',其还属于一个字符串,这样在解码的时候,只要去查找这个换行字符就可以了,参见代码如下:
解法三:
class Codec {
public:
// Encodes a list of strings to a single string.
string encode(vector<string>& strs) {
string res = "";
for (string str : strs) res += str + '\0';
return res;
}
// Decodes a single string to a list of strings.
vector<string> decode(string s) {
vector<string> res;
stringstream ss(s);
string t;
while (getline(ss, t, '\0')) {
res.push_back(t);
}
return res;
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/271
类似题目:
Serialize and Deserialize Binary Tree
参考资料:
https://leetcode.com/problems/encode-and-decode-strings/
https://leetcode.com/problems/encode-and-decode-strings/discuss/70412/AC-Java-Solution
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Encode and Decode Strings 加码解码字符串的更多相关文章
- [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 ...
- LeetCode Encode and Decode Strings
原题链接在这里:https://leetcode.com/problems/encode-and-decode-strings/ 题目: Design an algorithm to encode a ...
- [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 ...
- [LeetCode] Encode and Decode TinyURL 编码和解码精简URL地址
Note: This is a companion problem to the System Design problem: Design TinyURL. TinyURL is a URL sho ...
- 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 ...
- [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 ...
- 271. Encode and Decode Strings
题目: Design an algorithm to encode a list of strings to a string. The encoded string is then sent ove ...
- Encode and Decode Strings 解答
Question Design an algorithm to encode a list of strings to a string. The encoded string is then sen ...
- Encode and Decode Strings
Design an algorithm to encode a list of strings to a string. The encoded string is then sent over th ...
随机推荐
- 【分布式】Zookeeper序列化及通信协议
一.前言 前面介绍了Zookeeper的系统模型,下面进一步学习Zookeeper的底层序列化机制,Zookeeper的客户端与服务端之间会进行一系列的网络通信来实现数据传输,Zookeeper使用J ...
- new/delete重载
在c++中,有时我们需要在运行阶段为一个变量分配未命名的内存,并使用指针来访问它,这里就可以用到new关键字.另外需要指出的是,new分配的内存块通常与常规变量分配的内存块不同,常规变量的值都储存在被 ...
- ubuntu Chromium 安装 pepperflashplugin
sudo apt-get update sudo apt-get install chromium-browser sudo apt-get install pepperflashplugin-non ...
- PALIN - The Next Palindrome 对称的数
A positive integer is called a palindrome if its representation in the decimal system is the same wh ...
- No.004:Median of Two Sorted Arrays
问题: There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the ...
- Mybatis配置一对多的关联关系(五)
问题:是查询一个部门中的员工? 一.web项目构架 二.lib文件的jar 三.配置大小配置和该工具类 1大配置mybatis-config.xml <?xml version="1. ...
- Atitit java集成内嵌浏览器与外嵌浏览器attilax总结
Atitit java集成内嵌浏览器与外嵌浏览器attilax总结 HTML5将颠覆原生App世界.这听起来有点危言耸听,但若认真分析HTML5的发展史,你会发现,这个世界的发展趋势确实就是这样. 熟 ...
- 配置rsync服务,数据同步。
这部分设计服务器端和客户端. [服务器端] 如果服务器没有安装rsync服务则使用yum安装rsync服务. yum install rsync 然后 vim /etc/xinetd.d/rsync ...
- java函数的笔记
java中,函数即方法.也就是实现某个功能的办法. 函数的格式 修饰符 返回值类型 函数名(参数类型 参数) { 逻辑处理; return 处理结果; // return关键字是用于结束该函数的,并将 ...
- 微信小程序注册
小程序是一种新的开放能力,可以在微信内被便捷地获取和传播,同时具有出色的使用体验.开发者可以根据平台提供的能力,快速地开发一个小程序. 开放内容包括: 1.开放注册范围:企业.政府.媒体.其他组织: ...