Accepted Java Solution


  • 0
    T
    public class Codec {
    
    // Encodes a list of strings to a single string.
            public String encode(List<String> strs) {
            if (strs.isEmpty())
                return null;
            StringBuilder sb = new StringBuilder();
            strs.forEach(str -> {
                sb.append(str);
                sb.append("\n");
            });
            return sb.toString();
        }
    
        // Decodes a single string to a list of strings.
        public List<String> decode(String s) {
            List<String> strs = new ArrayList<String>();
            if (s == null)
                return strs;
            String[] arr = s.split("\n", -1);
            for(int i = 0 ; i < arr.length - 1; i++) {
                strs.add(arr[i]);
            }
            return strs;
        }
    }
    

Log in to reply
 

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