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的更多相关文章

  1. 271. Encode and Decode Strings

    题目: Design an algorithm to encode a list of strings to a string. The encoded string is then sent ove ...

  2. [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 ...

  3. [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 ...

  4. [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 ...

  5. LeetCode Encode and Decode Strings

    原题链接在这里:https://leetcode.com/problems/encode-and-decode-strings/ 题目: Design an algorithm to encode a ...

  6. Encode and Decode Strings

    Design an algorithm to encode a list of strings to a string. The encoded string is then sent over th ...

  7. Encode and Decode Strings 解答

    Question Design an algorithm to encode a list of strings to a string. The encoded string is then sen ...

  8. [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 ...

  9. 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 ...

随机推荐

  1. INSTALL_FAILED_SHARED_USER_INCOMPATIBLE错误解决

    Target device: smartisan-yq601-3fa1a5dcInstalling APK: /Users/wangliang/workspace/emm-android/build/ ...

  2. PAT Advanced 1003 Emergency (25) [Dijkstra算法]

    题目 As an emergency rescue team leader of a city, you are given a special map of your country. The ma ...

  3. part8 vue内置标签keep-alive对网页性能优化

    我们网页请求时候 我们点击路由切换 可以看network中数据请求 因为每次路由切换都会执行mounted钩子函数 我们这个函数中数据请求 //每次切换路由,页面都会重新渲染 在根组件中使用 路由切换 ...

  4. 了解redis

    redis:非关系型数据库,基于内存高性能,key-value存储,一般用作缓存,开源的使用ANSI C语言编写,遵守BSD协议,支持网络,可基于内存亦可持久化的日志型.Key-Value数据库,并提 ...

  5. 反编译查看printf()的方法

    源代码: package test2; public class ExplorationJDKSource { /** * @param args */ public static void main ...

  6. 吴裕雄--天生自然 JAVASCRIPT开发学习: DOM

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  7. 重载(overloading)和重写@Override

    一.重写:@Override 定义:字类方法覆盖父类方法,通俗来说就是方法里面的内容可以不一样,其他都一样. (1)必须保证权限大于等于父类的权限public>protetcted>默认& ...

  8. Codeforce 370C Mittens 巧妙数学题

    这道题目我一开始想错了,觉得只要排好序,再从头到尾把可以相互交换的进行下交换就可以了...事实证明是错的.正确的解法比较巧妙,而且写法非常好,值得学习 首先,要注意的一个规律是,假如最大的颜色数字出现 ...

  9. Charles 安装配置

    与Fiddler相比,Charles 对url的分类列表更为清晰,这一点是我更喜欢Charles的一点.当然在抓app包上,个人觉得使用Charles更得心应手,这一点可能仁者见仁智者见智. 下载 官 ...

  10. sklearn KMeans聚类算法(总结)

    基本原理 Kmeans是无监督学习的代表,没有所谓的Y.主要目的是分类,分类的依据就是样本之间的距离.比如要分为K类.步骤是: 随机选取K个点. 计算每个点到K个质心的距离,分成K个簇. 计算K个簇样 ...