LeetCode Encode and Decode Strings
原题链接在这里:https://leetcode.com/problems/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.
题解:
encode 时维护一个stringbuilder, 对于每一个string, sb append 该string长度 + "/" + string内容.
decode 时开始index = 0, while index < s.length(), 利用indexOf("/", fromIndex)找index 后面第一个 "/"的index, index 与 "/"index 之间是长度,解析后面该长度的string, 更新index到"/"index + 1 + len.
Time Complexity: encode, O(n). decode, O(n). n是strs list的所有string包含所有char的个数.
Space: O(n). StringBuilder sb 大小.
AC Java:
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).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<String>();
if(s == null || s.length() == 0){
return res;
}
int index = 0;
while(index < s.length()){
int forwardInd = s.indexOf("/", index);
int len = Integer.valueOf(s.substring(index, forwardInd));
res.add(s.substring(forwardInd+1,forwardInd+1+len));
index = forwardInd + 1 + len;
}
return res;
}
} // Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.decode(codec.encode(strs));
类似Serialize and Deserialize Binary Tree, Serialize and Deserialize N-ary Tree.
LeetCode 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] 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#271] Encode and Decode Strings
Problem: Design an algorithm to encode a list of strings to a string. The encoded string is then sen ...
- 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 ...
- 271. Encode and Decode Strings
题目: Design an algorithm to encode a list of strings to a string. The encoded string is then sent ove ...
- [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
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 ...
- [LeetCode] Encode and Decode TinyURL 编码和解码精简URL地址
Note: This is a companion problem to the System Design problem: Design TinyURL. TinyURL is a URL sho ...
随机推荐
- topcoder SRM 617 DIV2 SlimeXSlimonadeTycoon
此题需要注意的两个地方是 (1)在某天生产出来的Slimonades,必须在stale_limit天内必须卖完,否则超过stale_limit内抛弃(东西都有保质期) (2)每天生产出来的Slimon ...
- ACM: Hotel 解题报告 - 线段树-区间合并
Hotel Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Description The ...
- javascript new function()
// 执行的func生成一个空对象,如果有return就会把生成的func对象抛弃function func() {} alert(temp instanceof func) function fun ...
- [BZOJ2791][Poi2012]Rendezvous
2791: [Poi2012]Rendezvous Time Limit: 25 Sec Memory Limit: 128 MBSubmit: 95 Solved: 71[Submit][Sta ...
- BZOJ4127: Abs
Description 给定一棵树,设计数据结构支持以下操作 1 u v d 表示将路径 (u,v) 加d 2 u v 表示询问路径 (u,v) 上点权绝对值的和 Input 第一行两个整数n和m,表 ...
- Jquery_AjaxFileUpload插件的使用记录
功能需求:Spring MVC框架下,实现无刷新页面上传图片,并展示图片预览效果 直接上代码: 1.图片预览效果 <%@ page contentType="text/html;cha ...
- (转载)zeromq使用注意点滴
zeromq使用注意点滴 1.关于介绍zeromq的就不说了,可以自己去看官方guide很详细 2.主要说下在使用过程中需要注意的地方 1)使用如果使用c++的接口的时候,在你自己的类中或者apach ...
- 队列 Soldier and Cards
Soldier and Cards 题目: Description Two bored soldiers are playing card war. Their card deck consists ...
- OpenCV 3.1 StereoBM 获取正确视差Dispariy
OpenCV更新到3.0版本后,Stereo模块变化的挺多的,首先去掉了StereoBMState和StereoSGBMState这两个专门控制BM和SGBM算法参数的类,而且StereoBM不能直接 ...
- Qweb Pdf 中添加 图片
具体方法如下: <img t-if="company.logo" t-att-src="'data:image/png;base64,%s' % company.l ...