[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 ...
随机推荐
- 安装scrapy 爬虫框架
安装scrapy 爬虫框架 个人根据学习需要,在Windows搭建scrapy爬虫框架,搭建过程种遇到个别问题,共享出来作为记录. 1.安装python 2.7 1.1下载 下载地址 1.2配置环境变 ...
- 2020/2/4 PHP代码审计之会话认证漏洞
0x00 会话认证漏洞简介 会话认证是个非常大的话题,涉及各种协议和框架,如cookie.session.sso.oauth.openid等. 而其中最常使用的是Cookie和Session,他们都能 ...
- SpringBoot+Shiro+DB (二)
之前我们完成了Spring+Shiro的最基本配置搭建,现在我们再增加上DB,毕竟没有哪个系统会将用户.角色.权限等信息硬编码到代码里.DB选用myslq. 数据库准备 脚本如下.依然是两个用户:ad ...
- 腾讯大佬告诉你,写Python到底用什么IDE合适
不管你是 Python 新手还是老鸟,肯定纠结过一个问题: 到底用什么编辑器写 Python 代码好? 为此,我们调查了数十位鹅厂程序猿们爱用的 Python IDE,从他们对每款编辑器的看法中,也许 ...
- 干货 | 京东云Kubernetes集群+Traefik实战
摘要 Traefik支持丰富的annotations配置,可配置众多出色的特性,例如:自动熔断.负载均衡策略.黑名单.白名单.所以Traefik对于微服务来说简直就是一神器. 利用Traefik,并结 ...
- 201609-2 火车购票 Java
思路待补充 import java.util.Scanner; class Main{ public static void main(String[] args) { //100个座位 int[] ...
- 苹果下架2.5万赌博APP!一场净化风暴正在迅速成型
当下智能手机发展得如火如荼,但对于大众来说,体验终究还是要落到包罗万千的APP上.APP身为智能手机的灵魂,全面渗入了大众的工作.生活.娱乐.学习等多个方面.每一个APP的背后,其实都在打开着一扇通往 ...
- Vue.js——6.创建组件
Vue组件组件就是为了拆分Vue实例的代码量,能够不同的功能定义不同的组件创建组件的方法 1. // 创建组件 let com1=Vue.extend({ template:'<h1>he ...
- // 生成modbus CRC16数据
CRC- / MODBUS : )CRC寄存器初始值为 FFFF:即16个字节全为1: )CRC- / MODBUS的多项式A001H ( 0001B) ‘H’表示16进制数,‘B’表示二进制数 计算 ...
- Django知识点集合