资讯专栏INFORMATION COLUMN

[Leetcode] Encode and Decode Strings 字符串编解码

gself / 3230人阅读

摘要:记录长度法复杂度时间空间思路本题难点在于如何在合并后的字符串中,区分出原来的每一个子串。这里我采取的编码方式,是将每个子串的长度先赋在前面,然后用一个隔开长度和子串本身。这样我们先读出长度,就知道该读取多少个字符作为子串了。

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 strs) { // ... your code return encoded_string; }
Machine 2 (receiver) has the function:
vector decode(string s) { //... your code return strs; }

So Machine 1 does:
string encoded_string = encode(strs);
and Machine 2 does:
vector 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.

记录长度法 复杂度

时间 O(N) 空间 O(1)

思路

本题难点在于如何在合并后的字符串中,区分出原来的每一个子串。这里我采取的编码方式,是将每个子串的长度先赋在前面,然后用一个#隔开长度和子串本身。这样我们先读出长度,就知道该读取多少个字符作为子串了。

注意

为了简化代码,这里使用了indexOfsubstring这两个属于String的API,实际上复杂度和遍历是一样的。

代码
public class Codec {

    // Encodes a list of strings to a single string.
    public String encode(List strs) {
        StringBuilder output = new StringBuilder();
        for(String str : strs){
            // 对于每个子串,先把其长度放在前面,用#隔开
            output.append(String.valueOf(str.length())+"#");
            // 再把子串本身放在后面
            output.append(str);
        }
        return output.toString();
    }

    // Decodes a single string to a list of strings.
    public List decode(String s) {
        List res = new LinkedList();
        int start = 0;
        while(start < s.length()){
            // 找到从start开始的第一个#,这个#前面是长度
            int idx = s.indexOf("#", start);
            int size = Integer.parseInt(s.substring(start, idx));
            // 根据这个长度截取子串
            res.add(s.substring(idx + 1, idx + size + 1));
            // 更新start为子串后面一个位置
            start = idx + size + 1;
        }
        return res;
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/64585.html

相关文章

  • Python2.x 字符码终极指南

    摘要:值得注意的是,有的编码方案不一定能表示某些信息,这时编码就会失败,比如就不能用来表示中文。数组的每一项是一个字节,用来表示。所以对于字符串来说,其长度等于编码后字节的长度。所以,让来编码解码中文,就超出了其能力范围。 在人机交互之字符编码 一文中对字符编码进行了详细的讨论,并通过一些简单的小程序验证了我们对于字符编码的认识。但仅了解这篇文章的内容,并不能帮我们在日常编程中躲过一些字符编...

    Amio 评论0 收藏0
  • Python “黑魔法” 之 Encoding & Decoding

    摘要:我可以明确告诉你这不是,但它可以用解释器运行。这种黑魔法,还要从说起。提案者设想使用一种特殊的文件首注释,用于指定代码的编码。暴露了一个函数,用于注册自定义编码。所谓的黑魔法其实并不神秘,照猫画虎定义好相应的接口即可。 首发于我的博客,转载请注明出处 写在前面 本文为科普文 本文中的例子在 Ubuntu 14.04 / Python 2.7.11 下运行成功,Python 3+ 的接...

    邹强 评论0 收藏0
  • 字符码问题记录

    摘要:需求问题需要对序列化以后的对象中的在中进行存取由于声称只支持作为暴露出来的最基本的数据类型形式的存取所以需要在存取前后将与互相转换发现从出来的跟之前的不一样即使强制指定了一致的编码解码方式结果仍不符合预期猜测尝试怀疑是系统的默认编码方式与解 需求&问题 需要对序列化以后的对象 (java中的byte[]) 在redis中进行存取由于redis声称只支持String(作为redis暴露出...

    doodlewind 评论0 收藏0
  • 关于python的解码(decode, encode)

    摘要:,,等属于不同的字符集,转换编码就是在它们中的任意两者间进行。一般个人用的电脑上控制台基本上都是编码的,但运维的机器上基本全是,中文的时候就会有酸爽的问题。 总结总结,本文仅适用于python2.x 默认编码与开头声明 首先是开头的地方声明编码 # coding: utf8 这个东西的用处是声明文件编码为utf8(要写在前两行内),不然文件里如果有中文,比如 a = 美丽 b = u美...

    shusen 评论0 收藏0

发表评论

0条评论

gself

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<