[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 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;
- }
- public class Codec {
- // Encodes a list of strings to a single string.
- public String encode(List<String> strs) {
- StringBuilder sb = new StringBuilder();
- for (String str: strs) {
- int len = str.length();
- sb.append(len).append("/").append(str);
- }
- return sb.toString();
- }
- // Decodes a single string to a list of strings.
- public List<String> decode(String s) {
- List<String> res = new ArrayList<>();
- int i = 0;
- while (i < s.length()) {
- int slash = s.indexOf("/", i);
- int len = Integer.valueOf(s.substring(i, slash));
- res.add(s.substring(slash + 1, slash + 1 + len));
- i = slash + 1 + len;
- }
- return res;
- }
- }
- // Your Codec object will be instantiated and called as such:
- // Codec codec = new Codec();
- // codec.decode(codec.encode(strs));
[LC] 271. Encode and Decode Strings的更多相关文章
- 271. Encode and Decode Strings
题目: Design an algorithm to encode a list of strings to a string. The encoded string is then sent ove ...
- [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 ...
- [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 ...
随机推荐
- CSU2004:Finding words(含指定不相交前后缀的模式串计数)
题:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=2004 题意:给定n个模式串,m个询问,每个询问是“前缀+‘*’+后缀 ”的组合的串S,输出 ...
- ACWING基础算法(三)
双指针算法. 相向双指针,指的是在算法的一开始,两根指针分别位于数组/字符串的两端,并相向行走. ACWING 的一道裸题(不知道为啥进不去404):最长连续不重复子序列 输入 5 1 2 2 3 5 ...
- NRF24L01中断双向传输数据
NRF24L01是一款比较常见的无线通讯芯片,不过有个缺点就是只能半双工通讯,当涉及到双向通讯时就比较麻烦一些·,特别是想要做无线IAP数据需要一直来回发送,这点无疑然人恶心到想吐,不过还好有数据中断 ...
- 吴裕雄--天生自然MySQL学习笔记:MySQL 插入数据
MySQL 表中使用 INSERT INTO SQL语句来插入数据. 可以通过 mysql> 命令提示窗口中向数据表中插入数据,或者通过PHP脚本来插入数据. 以下为向MySQL数据表插入数据通 ...
- 1月18日 LCA专项训练
A. Lorenzo Von Matterhorn B.Minimum spanning tree for each edge C.Misha, Grisha and Underground D.Fo ...
- 2019.11.18CTFD搭建记录
### 0x01 实验室纳新,准备在自己服务器搭建个ctfd给新生们玩玩,忙活了一天orz[大一刚开学就搭建过没这么费力啊..] 现在大二了没想到能折腾一天... 直接说下我踩的坑吧,给后来的人们说说 ...
- tar.xz文件
创建或解压tar.xz文件的方法 习惯了 tar czvf 或 tar xzvf 的人可能碰到 tar.xz也会想用单一命令搞定解压或压缩.其实不行 tar里面没有征对xz格式的参数比如 z是针对 g ...
- windows 安装Bitcoin Core使用
1.官网下载https://bitcoin.org/en/download 选择Windows 其他系统就选择对应的就好 2.双击安装完过后,进入bin目录,打开bitcoin-qt.exe运行,提 ...
- PAT Advanced 1127 ZigZagging on a Tree (30) [中序后序建树,层序遍历]
题目 Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree c ...
- Java之多线程窗口卖票问题(Thread)
/** * * 例子:创建三个窗口卖票,总票数为100张.使用继承Thread类的方式 * * 存在线程的安全问题,待解决. * */class Window extends Thread{ priv ...