[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 ...
随机推荐
- c# 异步和同步 多线程
在执行较为耗时的处理时,很容易出现用户界面“卡顿”现象,用异步编程模型,将耗时处理的代码放到另一个线程上执行,不会阻止用户界面线程的继续执行,应用程序 就不再出现“卡顿”现象. 本例子提供同步加载和异 ...
- UML-持久框架-目标&关键思想
1.目标 1).使用模板方法.状态模式.命令模式来设计部分框架 2).介绍对象-关系(O-R)映射中的一些问题 3).使用虚代理实现的滞后具体化 2.关键思想 1).映射(Mapping) 类--表 ...
- python刷LeetCode:26. 删除排序数组中的重复项
难度等级:简单 题目描述: 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外 ...
- 给普通用户加sudo权限
系统环境:centos 7.0 引文:在实验室的服务器上给每个人分配了一个账号,但是有的时候普通用户需要使用root权限,比如装一些软件之类的.下面介绍怎么给普通用户添加sudo命令权限. 前提: s ...
- 74)搭建TCP服务器
补充: 1-->listen是监听,就是监听你建立的那个socket那个窗口,要是有客户端来了,那么就把他加到 队列里面,然后accept是从队列中取客户端(就是把对应的客人的信息拿过来,交给w ...
- 寒假day11
毕设数据抽取部分还有一点问题,正在修改中
- 基于springboot实现Ueditor并生成.html的示例
一.项目架构 二.项目代码 1.HtmlProductController.java package com.controller; import java.io.File; import java. ...
- Python说文解字_计数器
from collections import Counter response = [ "vanilla", "chocolate", "vanil ...
- 01 语言基础+高级:1-8 File类与IO流_day09【字节流、字符流】
day09[字节流.字符流] 主要内容 IO流 字节流 字符流 异常处理 Properties 教学目标 能够说出IO流的分类和功能 能够使用字节输出流写出数据到文件 能够使用字节输入流读取数据到程序 ...
- Sublime Text3 作Markdown编辑器 配置
准备 Sublime 配置 1 Package Control 2 安装插件 测试 其他 1. 准备 Windows操作系统 Sublime Text3 编辑器 Web浏览器 2. Sublime 配 ...