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
evalor serialize methods. You should implement your own encode/decode algorithm.
链接: http://leetcode.com/problems/encode-and-decode-strings/
题解:
encode and decode。这里我们可以维护一个StringBuilder,读出每个input string的长度,append一个特殊字符,例如'/',再append string。这样再decode的时候我们就可以利用java的String.indexOf(char,startIndex)来算出自startIndex其第一个'/'的位置,同时计算出接下来读取的string长度,用String.substring()读出字符串以后我们更新index,来进行下一次读取。 这些只是简单地encode/decode,至于加密之类的还需要学习Cousera上的Crytography I和II, 作业很难,希望下次开课能坚持下去。
Time Complexity - O(n), Space Complexity - O(1)
public class Codec {
// Encodes a list of strings to a single string.
public String encode(List<String> strs) {
if(strs == null || strs.size() == 0) {
return "";
}
StringBuilder sb = new StringBuilder();
for(String s : strs) {
int len = s.length();
sb.append(len);
sb.append('/');
sb.append(s);
}
return sb.toString();
}
// Decodes a single string to a list of strings.
public List<String> decode(String s) {
List<String> res = new ArrayList<>();
if(s == null ||s.length() == 0) {
return res;
}
int index = 0;
while(index < s.length()) {
int forwardSlashIndex = s.indexOf('/', index);
int len = Integer.parseInt(s.substring(index, forwardSlashIndex));
res.add(s.substring(forwardSlashIndex + 1, forwardSlashIndex + 1 + len));
index = forwardSlashIndex + 1 + len;
}
return res;
}
}
// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.decode(codec.encode(strs));
二刷:
这回也写得比较快。
在encode时我们可以对strs先append长度,再append一个delimiter,最后append目标字符串。
在decode时我们从头遍历String s,先保存一个sliding window的左端点lo,遇到delimiter的时候,我们回头去找这个字符串的长度,也就是s.substring(lo, i)。之后我们按照这个长度,把字符串extract出来,并且加入到结果集里,再更新lo以及i。最后返回结果就可以了。
稍快一点的方法可能是在decode时把字符串转换为数组然后处理,但原理大都一致。
Java:
Time Complexity - O(n), Space Complexity - O(n)
public class Codec {
// Encodes a list of strings to a single string.
public String encode(List<String> strs) {
StringBuilder sb = new StringBuilder();
for (String s : strs) {
sb.append(s.length()).append('#').append(s);
}
return sb.toString();
}
// Decodes a single string to a list of strings.
public List<String> decode(String s) {
List<String> res = new ArrayList<>();
if (s == null || s.length() == 0) return res;
for (int lo = 0, i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '#') {
int len = Integer.parseInt(s.substring(lo, i));
res.add(s.substring(i + 1, i + 1 + len));
lo = i + 1 + len;
i = i + 1 + len;
}
}
return res;
}
}
// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.decode(codec.encode(strs));
Reference:
https://leetcode.com/discuss/55020/ac-java-solution
https://leetcode.com/discuss/59840/clean-code-standard-way-of-serialization-deserialization
https://leetcode.com/discuss/57890/1-7-lines-python-length-prefixes
https://leetcode.com/discuss/54906/accepted-simple-c-solution
271. Encode and Decode Strings的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- Encode and Decode Strings 解答
Question 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 ...
随机推荐
- 每日会议<第二天>
昨天:看android编程教学视频 今天:看了老师给的学姐.学长软件的代码,找到他们的优缺点,取长补短 困难:创新意识少,提不出建设性意见,缺少自己的思想
- java基础学习之对象转型
对象转型(casting): 1)一个基类的引用类型变量可以指向其子类的对象 2)一个基类的引用不可以访问其子类对象新增加的成员(属性和方法) 3)可以使用引用变量instanceof类名,来判断该引 ...
- 【BZOJ】【1003】【ZJOI2006】物流运输trans
最短路/DP 这题数据规模并不大!!这是重点……… 所以直接暴力DP就好了:f[i]表示前 i 天的最小花费,则有$f[i]=min\{f[j]+cost[j+1][i]+k\} (0\leq j \ ...
- 【转载】在程序中动态改变static text控件的caption值
方法1,给STATIC控件取个名字叫IDC_STATICTITLE 然后在ClassWizard中设定一个控件变量给它叫m_statictitle 然后用m_statictitle.SetWindow ...
- Js高程笔记->引用类型
1 . Object 对象 2 . Array 对象 : 检测方法:ES5 : isArray 转换方法: toLocaleString , toString , val ...
- nmap所有参数详解
QQ:1258496116 端口:80http 443https 53dns 25smtp 22ssh 23telnet20.21ftp 110pop3 119nntp 143imap 179bgp ...
- 疯狂java讲义——继承
本文章只是记录我在学习疯狂java讲义里面,对之前java知识查缺补漏进行的总结. 方法重写 方法重写要遵循"两同两小一大"规则."两同"即方法名相同.形参列表 ...
- close和shutdown的区别
转的,没验证 close(sock_fd)会把sock_fd的内部计数器减1当sock_fd的内部计数器为0时, 才调用shutodwn(), 并最终释放文件描述符调用shutdown()只是进行了T ...
- 谈JavaScript组合拼接字符串的效率 --转载
JavaScript组合拼接字符串的效率.在脚本开发过程中,经常会按照某个规则,组合拼接出一个大字符串进行输出.比如写脚本控件时控制整个控件的外观的HTML标签输出,比如AJAX里得到服务器端的回传值 ...
- POJ 1325
#include<iostream> #include<stdio.h> #define MAXN 105 using namespace std; int _m[MAXN][ ...