zoukankan      html  css  js  c++  java
  • 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;
    }

    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.

    链接: http://leetcode.com/problems/encode-and-decode-strings/

    题解:

    encode and decode。这里我们可以维护一个StringBuilder,读出每个input string的长度,append一个特殊字符,例如'/',再append string。这样再decode的时候我们就可以利用java的String.indexOf(char,startIndex)来算出自startIndex其第一个'/'的位置,同时计算出接下来读取的string长度,用String.substring()读出字符串以后我们更新index,来进行下一次读取。 这些只是简单地encode/decode,至于加密之类的还需要学习Cousera上的Crytography I和II, 作业很难,希望下次开课能坚持下去。

    Time Complexity - O(n), Space Complexity - O(1)

    public class Codec {
    
        // Encodes a list of strings to a single string.
        public String encode(List<String> strs) {
            if(strs == null || strs.size() == 0) {
                return "";
            }
            StringBuilder sb = new StringBuilder();
            for(String s : strs) {
                int len = s.length();
                sb.append(len);
                sb.append('/');
                sb.append(s);
            }
            return sb.toString();
        }
    
        // Decodes a single string to a list of strings.
        public List<String> decode(String s) {
            List<String> res = new ArrayList<>();
            if(s == null ||s.length() == 0) {
                return res;
            }
            int index = 0;
            while(index < s.length()) {
                int forwardSlashIndex = s.indexOf('/', index);
                int len = Integer.parseInt(s.substring(index, forwardSlashIndex));
                res.add(s.substring(forwardSlashIndex + 1, forwardSlashIndex + 1 + len));
                index = forwardSlashIndex + 1 + len;
            }
            return res;
        }
    }
    
    // Your Codec object will be instantiated and called as such:
    // Codec codec = new Codec();
    // codec.decode(codec.encode(strs));

    二刷:

    这回也写得比较快。

    在encode时我们可以对strs先append长度,再append一个delimiter,最后append目标字符串。

    在decode时我们从头遍历String s,先保存一个sliding window的左端点lo,遇到delimiter的时候,我们回头去找这个字符串的长度,也就是s.substring(lo, i)。之后我们按照这个长度,把字符串extract出来,并且加入到结果集里,再更新lo以及i。最后返回结果就可以了。

    稍快一点的方法可能是在decode时把字符串转换为数组然后处理,但原理大都一致。

    Java:

    Time Complexity - O(n), Space Complexity - O(n)

    public class Codec {
    
        // Encodes a list of strings to a single string.
        public String encode(List<String> strs) {
            StringBuilder sb = new StringBuilder();
            for (String s : strs) {
                sb.append(s.length()).append('#').append(s);
            }
            return sb.toString();
        }
    
        // Decodes a single string to a list of strings.
        public List<String> decode(String s) {
            List<String> res = new ArrayList<>();
            if (s == null || s.length() == 0) return res;
            for (int lo = 0, i = 0; i < s.length(); i++) {
                char c = s.charAt(i);
                if (c == '#') {
                    int len = Integer.parseInt(s.substring(lo, i));
                    res.add(s.substring(i + 1, i + 1 + len));
                    lo = i + 1 + len;
                    i = i + 1 + len;
                }
            }
            return res;
        }
    }
    
    // Your Codec object will be instantiated and called as such:
    // Codec codec = new Codec();
    // codec.decode(codec.encode(strs));

    Reference:

    https://leetcode.com/discuss/55020/ac-java-solution

    https://leetcode.com/discuss/59840/clean-code-standard-way-of-serialization-deserialization

    https://leetcode.com/discuss/57890/1-7-lines-python-length-prefixes

    https://leetcode.com/discuss/54906/accepted-simple-c-solution

  • 相关阅读:
    java 数组转list的两种方式(可新增和删除list元素)
    SpringBoot配置404跳转页面的两种方式
    idea java常量字符串过长解决办法
    Spring-BeanValidation校验@RequestParam参数 (控制器单参数验证)
    【Java】使用@Valid+BindingResult进行controller参数校验
    Spring MVC利用Hibernate Validator实现后端数据校验
    springMvc 整合hibernate-validator(简单配置)
    vue中动态给自定义属性data-xx赋值并读取内容
    Tomcat配置SSL安全证书
    springmvc 接收json对象的两种方式
  • 原文地址:https://www.cnblogs.com/yrbbest/p/5027985.html
Copyright © 2011-2022 走看看