java 271.编码和解码Strings.java

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 271.编码和解码Strings.java相关的知识,希望对你有一定的参考价值。

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) {
            sb.append(str.length()).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<String>();
        int len = s.length();
        int i = 0;
        while (i < len) {
            int val = 0;
            while (s.charAt(i) >= '0' && s.charAt(i) <= '9') {
                val = val * 10 + s.charAt(i++) - '0';
            }
            res.add(s.substring(++i, i + val));
            i += val;
        }
        return res;
    }
}

// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.decode(codec.encode(strs));

以上是关于java 271.编码和解码Strings.java的主要内容,如果未能解决你的问题,请参考以下文章

java 271.编码和解码Strings.java

java 271.编码和解码Strings.java

java 271.编码和解码Strings.java

java 271.编码和解码Strings.java

271. 字符串的编码与解码

java中编码和解码的问题