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;
}

Machine 2 (receiver) has the function:

vector<string> decode(string s) {
//... your code
return strs;
}

So Machine 1 does:

string encoded_string = encode(strs);

and Machine 2 does:

vector<string> strs2 = decode(encoded_string);

strs2 in Machine 2 should be the same as strs in Machine 1.

Implement the encode and decode methods.

Note:

  • The string may contain any possible characters out of 256 valid ascii characters. Your algorithm should be generalized enough to work on any possible characters.
  • Do not use class member/global/static variables to store states. Your encode and decode algorithms should be stateless.
  • Do not rely on any library method such as eval or serialize methods. You should implement your own encode/decode algorithm.

分析:

count + "#"

 public class Solution {

     public String[] decode(String str) {
ArrayList<String> list = new ArrayList<String>();
int pointer = ;
while (pointer < str.length()) {
int end = pointer;
while (str.charAt(end) != '#') {
end++;
}
int count = Integer.parseInt(str.substring(pointer, end));
list.add(str.substring(end + , end + + count));
pointer = end + + count;
}
return list.toArray(new String[list.size()]);
} public String encode(String[] strs) {
StringBuilder sb = new StringBuilder();
for (String s : strs) {
sb.append(s.length());
sb.append("#");
sb.append(s);
}
return sb.toString();
}
}

Encode and Decode Strings的更多相关文章

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

  2. LeetCode Encode and Decode Strings

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

  3. 271. Encode and Decode Strings

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

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

  5. Encode and Decode Strings 解答

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

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

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

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

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

随机推荐

  1. Java常见异常

    1. runtimeException子类: 1. java.lang.ArrayIndexOutOfBoundsException    数组索引越界异常.当对数组的索引值为负数或大于等于数组大小时 ...

  2. Java基础-JDK动态代理

    JDK的动态代理依靠接口实现  代理模式 代理模式是常用的java设计模式,他的特征是代理类与委托类有同样的接口,代理类主要负责为委托类预处理消息.过滤消息.把消息转发给委托类,以及事后处理消息等.代 ...

  3. Java sun的JDK

    JDK概述 JDK(Java Development Kit)是Sun Microsystems针对Java开发员的产品.自从Java推出以来,JDK已经成为使用最广泛的Java SDK(Softwa ...

  4. BZOJ-3229 石子合并 GarsiaWachs算法

    经典DP?稳T 3229: [Sdoi2008]石子合并 Time Limit: 3 Sec Memory Limit: 128 MB Submit: 426 Solved: 202 [Submit] ...

  5. NOI题库

    07:机器翻译 总时间限制: 1000ms 内存限制: 65536kB 描述 小晨的电脑上安装了一个机器翻译软件,他经常用这个软件来翻译英语文章. 这个翻译软件的原理很简单,它只是从头到尾,依次将每个 ...

  6. cogs896 圈奶牛

    描述 农夫约翰想要建造一个围栏用来围住他的奶牛,可是他资金匮乏.他建造的围栏必须包括他的奶牛喜欢吃草的所有地点.对于给出的这些地点的坐标,计算最短的能够围住这些点的围栏的长度. PROGRAM NAM ...

  7. git使用记录

    唔,git有本地版本管理功能,所以,这个完全是可以拿来自己做版本管理的.所以有必要学习一下,另外,在oschina上开了个账户,用来管理自己一些代码,也是增加自己学习git的动力. 1. 使用clon ...

  8. Php学习之SESSION反序列化机制

    在php.ini中存在三项配置项:session.save_path="" --设置session的存储路径session.save_handler="" -- ...

  9. Yii2.0 执行流程分析

    1 index.php 2 ---->引入 vendor/auto_load.php 3 auto_load.php 4 ---->引入 ventor/composer/autoload_ ...

  10. IOS基础之 (九) Foundation框架

    一NSNumber 类型转换 NSNumber 把基本数据类型包装成一个对象类型.NSNumber之所以可以(只能)包装基本数据类型,是因为继承了NSValue. @20 等价于 [NSNumber ...