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保存 ...
随机推荐
- CF451E Devu and Flowers 解题报告
CF451E Devu and Flowers 题意: \(Devu\)有\(N\)个盒子,第\(i\)个盒子中有\(c_i\)枝花.同一个盒子内的花颜色相同,不同盒子的花颜色不同.\(Devu\)要 ...
- Angular 监听路由变化
var app = angular.module('Mywind',['ui.router']) //Angular 监听路由变化 function run($ionicPlatform, $loca ...
- 微信小程序使用Socket
首先,一个小程序同时只能有一个WebSocket连接,如果当前已经存在一个WebSocket连接,会关闭当前连接,并重新建立一个连接. 其次,如果使用了appID,协议必须是 wss://... 最近 ...
- codevs 1191 线段树 区间更新(水)
题目描述 Description 在一条数轴上有N个点,分别是1-N.一开始所有的点都被染成黑色.接着我们进行M次操作,第i次操作将[Li,Ri]这些点染成白色.请输出每个操作执行后剩余黑色点的个数. ...
- [POJ2777] Count Color
\[Count Color\] Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 50865 Accepted: 15346 Des ...
- iebackground+icon图标兼容
<!DOCTYPE > <html> <head> <title>zepto</title> <meta name="nam ...
- NOIP2005过河(青蛙过河)
题目传送门 这道题主要是因为L长度最大可以为1e9 而石子却最多只有100个 这样就浪费了很多时间空间 所以我们压缩一波路径就可以了 剩余的就是枚举每个点以及i-y到i-x的dp了 这里要说一句为什么 ...
- SQLSERVER数据库置疑、可疑、脱机、单用户、紧急模式等的修复
数据库出现置疑.可疑.脱机.单用户.紧急模式主要是因为数据库的日志文件除了问题,2000和2008修复方式不一样,2008的修复脚本在2000中不适用,主要是不被2000识别. 假设数据库名为:eis ...
- 【“10”力全开 游戏“Ti”厉害】ZX53VE-新飞行堡垒笔记本(Windows 10 Home/新七代标压i7-7700HQ/GTX 1050Ti 4G/8G内存/1TB+128GB)
[“10”力全开 游戏“Ti”厉害]ZX53VE-新飞行堡垒笔记本(Windows 10 Home/新七代标压i7-7700HQ/GTX 1050Ti 4G/8G内存/1TB+128GB) http: ...
- [ Openstack ] OpenStack-Mitaka 高可用之 镜像服务(glance)
目录 Openstack-Mitaka 高可用之 概述 Openstack-Mitaka 高可用之 环境初始化 Openstack-Mitaka 高可用之 Mariadb-Galera集群 ...