Encode and Decode Strings 解答
Question
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.
Solution 1 -- JSON format
第一种方法是参考的JSON的规则。
Encode: 我们将输入的字符串数组封装成JSON中的array。[ (left bracket) and ] (right bracket) 表示开头的结尾。中间的分隔用, (comma)。然后对于每个字符串,是 wrapped in double quotes。
由于字符串中本来就可能有双引号或是back slash (\),所以我们需要对这两种符号做转义。方法是多加一个back slash
如 原字符串 \"aafg" -> \\\"aafg\"
JSON里还有更复杂的字符串处理方法。但我们这里的目标只是让encode,再decode后的字符串相同,所以不必那么复杂。
Decode处理原则如下
1. 一个boolean的variable记录当前应该是下一个字符串的开头还是当前字符串的结束
2. 碰到bracket,根据是开始/结束,新建一个空字符串/将当前的字符串存入结果中
3. 碰到back slash,看它下一个元素是否是back slash / bracket,如果是,则将它下一个元素加到字符串中,计数加一。
public class Codec {
private final char start = '[';
private final char end = ']';
private final char include = '"';
private final char strSplit = ','; // Encodes a list of strings to a single string.
public String encode(List<String> strs) {
StringBuilder sb = new StringBuilder();
sb.append(start);
for (String str : strs) {
sb.append(include);
int len = str.length();
for (int i = 0; i < len; i++) {
char current = str.charAt(i);
if (current == '"' || current == '\\') {
sb.append('\\');
}
sb.append(current);
}
sb.append(include);
sb.append(strSplit);
}
sb.append(end);
return sb.toString();
} // Decodes a single string to a list of strings.
public List<String> decode(String s) {
List<String> result = new ArrayList<String>();
if (s == null || s.length() < 1) {
return result;
}
int len = s.length();
if (s.charAt(0) != start || s.charAt(len - 1) != end) {
return result;
}
boolean startSymbol = true;
StringBuilder sb = new StringBuilder();
for (int i = 1; i < len - 1; i++) {
char current = s.charAt(i);
if (current == include) {
if (startSymbol) {
sb = new StringBuilder();
} else {
result.add(sb.toString());
}
startSymbol = !startSymbol;
continue;
}
if (current == strSplit && startSymbol) {
continue;
}
if (current == '\\') {
char next = s.charAt(i + 1);
if (next == '\\' || next == '"') {
sb.append(next);
i++;
continue;
}
}
sb.append(current);
}
return result;
}
} // Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.decode(codec.encode(strs));
Solution 2
利用了Java里String的 int indexOf(int ch, int fromIndex)函数。
同时存入字符串和字符串的长度。
public class Codec { // Encodes a list of strings to a single string.
public String encode(List<String> strs) {
StringBuilder sb = new StringBuilder();
for (String str : strs) {
sb.append(str.length()).append('/').append(str);
}
return sb.toString();
} // Decodes a single string to a list of strings.
public List<String> decode(String s) {
List<String> result = new ArrayList<String>();
int length = s.length();
int i = 0;
while (i < length) {
int slash = s.indexOf('/', i);
int size = Integer.valueOf(s.substring(i, slash));
result.add(s.substring(slash + 1, slash + size + 1));
i = slash + size + 1;
}
return result;
}
}
Encode and Decode Strings 解答的更多相关文章
- [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 ...
- LeetCode Encode and Decode Strings
原题链接在这里:https://leetcode.com/problems/encode-and-decode-strings/ 题目: Design an algorithm to encode a ...
- Encode and Decode Strings
Design an algorithm to encode a list of strings to a string. The encoded string is then sent over th ...
- 271. Encode and Decode Strings
题目: Design an algorithm to encode a list of strings to a string. The encoded string is then sent ove ...
- [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 ...
- [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 ...
- 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 加码解码字符串
Design an algorithm to encode a list of strings to a string. The encoded string is then sent over th ...
- [LC] 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 ...
随机推荐
- hdu4521-小明系列问题——小明序列(线段树区间求最值)
题意:求最长上升序列的长度(LIS),但是要求相邻的两个数距离至少为d,数据范围较大,普通dp肯定TLE.线段树搞之就可以了,或者优化后的nlogn的dp. 代码为 线段树解法. #include ...
- [置顶] 单片机C语言易错知识点经验笔记
今天写这一篇文章并不是因为已经想好了一篇文章才写下来,而是我要将这一篇文章作为一个长期的笔记来写,我会一直更新.在进行单片机开发时,经常都会出现一些很不起眼的问题,这些问题其实都是很基础的c语言知识点 ...
- CGI、FastCGI和php-fpm的区别
参考网上的描述,将网上的描述内容,整理进来: 首先,CGI是干嘛的?CGI是为了保证web server传递过来的数据是标准格式的,方便CGI程序的编写者.web server(比如说nginx)只是 ...
- iOS 使用pods报错问题 pod --version
错误信息如下 find_spec_for_exe': can't find gem cocoapods (>= 0.a) (Gem::GemNotFoundException) from /Us ...
- unity的坑
http://dearymz.blog.163.com/blog/static/20565742013341916919/ 编辑器: Hierarchy窗口中是场景中的Game Object列表 Pr ...
- codeforces C. Cd and pwd commands 执行命令行
执行命令来改变路径 cd 并显示路径命令 pwd 一个节目的 抽样: input 7 pwd cd /home/vasya pwd cd .. pwd cd vasya/../petya pwd ou ...
- GridView出现不完整--GridView删除滚动条
GridView显示不完毕的原因是由于,他的外层也套用了一个滑动的控件,解决办法就是重写GridView,是控制GridView不能滚动,就是写一个类继承GridView 代码例如以下 public ...
- C#获取文件和文件夹大小
代码如下: /// <summary> /// 获取文件夹大小 /// </summary> /// <param name="dirPath"> ...
- vmware下给linux添加硬盘
http://blog.csdn.net/robbie1314520/archive/2010/08/10/5802724.aspx 创建虚拟硬盘 1.关闭VM中正在运行的虚拟系统: 2.EDIT V ...
- jquery之radio
<td class="queryTitle" width="80px">是否启用</td> <td class="que ...