9ms Java solution


  • 0
    B
        int[] len;
        // Encodes a list of strings to a single string.
        public String encode(List<String> strs) {
            StringBuilder sb = new StringBuilder();
            len = new int[strs.size()];
            int i = 0;
            for(String s : strs){
                len[i] = s.length();
                i++;
                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<String>();
            int cur = 0;
            for(int i=0;i<len.length;i++){
                res.add(s.substring(cur, cur+len[i]));
                cur = cur + len[i];
            }
            return res;
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.