Encode and Decode Strings -- LeetCode
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.
思路:每个字符串之前添加它的长度和一个%字符。
class Codec {
public: // Encodes a list of strings to a single string.
string encode(vector<string>& strs) {
string res;
for (int i = ; i < strs.size(); i++)
res += std::to_string(strs[i].size()) + "%" + strs[i];
return res;
} // Decodes a single string to a list of strings.
vector<string> decode(string s) {
vector<string> res;
int curInd = ;
int mark = s.find("%");
while (mark != std::string::npos) {
int len = std::stoi(s.substr(curInd, mark - curInd));
if (len) res.push_back(s.substr(mark + , len));
//in case it is an empty string
else res.push_back("");
curInd = mark + len + ;
mark = s.find("%", curInd);
}
return res;
}
}; // Your Codec object will be instantiated and called as such:
// Codec codec;
// codec.decode(codec.encode(strs));
Encode and Decode Strings -- LeetCode的更多相关文章
- [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 ...
- [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 ...
- 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 ...
- 535. Encode and Decode TinyURL - LeetCode
Question 535. Encode and Decode TinyURL Solution 题目大意:实现长链接加密成短链接,短链接解密成长链接 思路:加密成短链接+key,将长链接按key保存 ...
随机推荐
- Git远程仓库的使用(github为例)
一. 创建SSH key 输入命令“ssh-keygen –t rsa”创建ssh key. 由于笔者pc机已有ssh key,这里不再重复创建覆盖,仅做演示. 笔者创建好的s ...
- 停止ambari上服务的顺序
Before performing any upgrades or uninstalling software, stop all of the Hadoop services in the foll ...
- 使用Spring AOP实现读写分离(MySql实现主从复制)
1. 背景 我们一般应用对数据库而言都是“读多写少”,也就说对数据库读取数据的压力比较大,有一个思路就是说采用数据库集群的方案,其中一个是主库,负责写入数据,我们称之为:写库: 其它都是从库,负责读 ...
- Spring学习--通过注解配置 Bean (一)
在 classpath 中扫描组件: 组件扫描(component scanning): Spring 能够从 classpath 下自动扫描 , 侦测和实例化具有特定注解的组件. 特定组件包括: @ ...
- 布局之BFC
BFC 什么是BFC,在哪里需要用到BFC,BFC有什么规则?生成BFC有什么条件?这几个问题,我将为大家一一解释,下面我们进入正题. BFC(Block formatting context)直译为 ...
- [06] 盒模型 + auto 居中 + 垂直合并
1.盒模型 盒子模型有两种,分别是 ie 盒子模型和标准 w3c 盒子模型. 标准(W3C)模型中:CSS中的宽(width) = 内容 (content)的宽 CSS中的宽(width) = 内容( ...
- babel-preset-es2015,babel-polyfill 与 babel-plugin-transform-runtime
babel-preset-es2015 是一个babel的插件,用于将部分ES6 语法转换为ES5 语法.转换的语法包括: 箭头函数 var a1 = () => 1 编译为 var a1 = ...
- PHP等比例生成缩略图
/** * 生成缩略图 * $imgSrc 图片源路径 * $resize_width 图片宽度 * $resize_height 图片高度 * $dstimg 缩略图路径 * $isCut 是否剪切 ...
- idea 从远程仓库导入git项目
File--new --Project from vision control -- git 输入远程仓库地址 即可.
- 汕头市队赛 yyl杯1 T1
A SRM 05 - YYL 杯 R1 背景 傻逼题 描述 给一个序列,序列里只有两种元素1和2.现在要从序列里选出一些非空子序列使得子序列里两种元素数量相同.问有多少种方案数? 输入格式 多组数据 ...