[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 ...
随机推荐
- 2020/1/31 PHP代码审计之文件包含漏洞
0x00 文件包含简介 文件包含漏洞的产生原因是在通过引入文件时,引用的文件名,用户可控,由于传入的文件名没有经过合理的校检,或者校验被绕过,从而操作了预想之外的文件,就可能导致意外的文件泄露甚至恶意 ...
- POJ 2014:Flow Layout 模拟水题
Flow Layout Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 3091 Accepted: 2148 Descr ...
- vnpy交易接口学习
1.按照github中环境准备要求,配置好环境要求. https://github.com/vnpy/vnpy mongdb安装在D:\Program Files\MongoDB\Server\3.4 ...
- Codeforces Round #594 (Div. 1) Ivan the Fool and the Probability Theory
题意:给你一个NxM的图,让你求有多少符合 “一个格子最多只有一个同颜色邻居”的图? 题解:首先我们可以分析一维,很容易就可以知道这是一个斐波那契计数 因为dp[1][m]可以是dp[1][m-1]添 ...
- 201612-1 中间数 Java
思路: 先排序,两个count变量记录.有点暴力 import java.util.Arrays; import java.util.Scanner; public class Main { publ ...
- 201771010123汪慧和《面向对象程序设计JAVA》第九周实验总结
一.理论部分 1.异常 (1)异常处理的任务就是将控制权从错误产生的地方转移给能够处理这种情况的错误处理器. (2)程序中可能出现的错误和问题:a.用户输入错误.b.设备错误.c.物理限制.d.代码错 ...
- UOJ #2 【NOI2014】起床困难综合症
这道题我们设两个bitset(N和Y) \(N_i = cal(i,0) , Y_i=cal(i,1)\) cal(i) 即第i位经过题目中的计算后所得出来的值 然后贪心.倒序循环i,考虑第i位如何决 ...
- 【PXC】关于限流的参数,状态值说明
一.什么是流控(FC)?如何工作? 节点接收写集并把它们按照全局顺序组织起来,节点将接收到的未应用和提交的事务保存在接收队列中,当这个接收队列达到一定的大小,将触发限流:此时节点将暂停复制,节点会先处 ...
- 面试必问之http以及浏览器相关知识
/** 1.HTTP以及HTTPS概念 HTTP是超文本传输协议,是一个用于传输超媒体文档的应用层协议,被用于在web浏览器和网站服务器之间,以明文方式传递信息, 不提供任何方式的饿数据加密,因此使用 ...
- cookie保存
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...