LeetCode 271. Encode and Decode Strings
2016-05-24 12:41
141 查看
This is to encode the string not to COMPRESS the string.
This is one possible answer.
This is one possible answer.
#include <string> #include <vector> #include <iostream> using namespace std; string encode(vector<string>& strs) { string res = ""; for(int i = 0; i < strs.size(); ++i) { res += to_string(strs[i].size()) + "#" + strs[i]; } return res; } vector<string> decode(string s) { vector<string> res; int i = 0, j = 0; while(i < s.size()) { if(s[i] != '#') {i++; continue;} else { int len = stoi(s.substr(j, i - j)); res.push_back(s.substr(i + 1, len)); i = i + 1 + len; j = i; } } //while(s.size() > 0) { // int pos = s.find_first_of('#'); // if not know this function, better to use two pointers. // int len = stoi(s.substr(0, pos)); // res.push_back(s.substr(pos + 1, len)); // s = s.substr(pos + len + 1); //} return res; } int main(void) { vector<string> words{"tammy", "is", "a", "good", "girl"}; string s = encode(words); cout << endl; vector<string> res = decode(s); for(int i = 0; i < res.size(); ++i) cout << res[i] << endl; }
相关文章推荐
- redis主从+sentinel主从自动切换
- [WinForm]WinForm跨线程UI操作常用控件类大全
- 《linux内核设计与实现》实践之模块及深入
- 重置CentOS 7的root密码
- mysql Threads_created 增长过快的解决
- 第十三周项目—阅读程序,并运行结果(虚函数)
- Java版的双色球买彩票程序
- Android(进度条)异步更新UI的三种方式
- 【leetcode】20. Valid Parentheses
- [mac]添加系统自带辞典或下载的词典包
- ASP.NET——初步了解
- 定制班第九课 Receiver在Driver的精妙实现全生命周期彻
- Davinci DM6446开发攻略-UBOOT-2009.03移植2 nand flash的烧写
- 凌乱
- Redis常用命令
- ath10 移植记录
- iOS 苹果官方Demo合集
- 将string转为同名类名,方法名。(c#反射)
- 《善圈第27期天天圈》5.23精彩回顾:善圈微商,干货鸡汤!
- ubuntu14.04安装python3