271. Encode and Decode Strings

    /*
     * 271. Encode and Decode Strings
     * 2016-6-25 by Mingyang
     * 这道题很酷的地方就是选择一种存储方式可以有效地存储string,我一开始就想到了存长度加string的方法
     * 这个题用了一个indexof的API,
     * public int indexOf(String str,int fromIndex)
     * Returns the index within this string of the first occurrence of the specified substring,
     * starting at the specified index.The returned index is the smallest value k for which:
     * k >= fromIndex && this.startsWith(str, k)
     * 就是说从fromIndex以后的第一次出现str的index
     */
      // 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> ret = new ArrayList<String>();
        int i = 0;
        while(i < s.length()) {
            int slash = s.indexOf(‘/‘, i);
            int size = Integer.valueOf(s.substring(i, slash));
            ret.add(s.substring(slash + 1, slash + size + 1));
            i = slash + size + 1;
        }
        return ret;
    }
时间: 2024-10-14 08:52:54

271. Encode and Decode Strings的相关文章

[LeetCode#271] Encode and Decode Strings

Problem: 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) { //

Encode and Decode Strings 解答

Question 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) { //

Encode and Decode Strings -- LeetCode

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

LeetCode &quot;Encode and Decode Strings&quot;

This is about https://en.wikipedia.org/wiki/Run-length_encoding. The trick is, for a valid char, we only compress up to 254 occurences - count 255 means end of a string. typedef unsigned char UCHAR; class Codec { const static int MAX_CNT = 255; publi

iOS 7: Base64 Encode and Decode NSData and NSString Objects

iOS 7: Base64 Encode and Decode NSData and NSString Objects FRI, JAN 24 CORE SERVICES TWEET With the release of iOS 7, Apple added support for encoding and decoding data using Base64. In this post we will walk through two examples using Base64 to enc

python的str,unicode对象的encode和decode方法

python的str,unicode对象的encode和decode方法 python中的str对象其实就是"8-bit string" ,字节字符串,本质上类似java中的byte[]. 而python中的unicode对象应该才是等同于java中的String对象,或本质上是java的char[]. 对于 s="你好" u=u"你好" s="你好" u=u"你好" 1. s.decode方法和u.enc

Python之encode与decode浅析

 Python之encode与decode浅析 在 python 源代码文件中,如果你有用到非ASCII字符,则需要在文件头部进行字符编码的声明,声明如下: # code: UTF-8 因为python 只检查 #.coding 和编码字符串,为了美观等原因可以如下写法: #-*-coding:utf-8-*- 常见编码介绍: GB2312编码:适用于汉字处理.汉字通信等系统之间的信息交换. GBK编码:是汉字编码标准之一,是在 GB2312-80 标准基础上的内码扩展规范,使用了双字节编码.

JAVA 字符串题目 以静态方法实现encode()和decode()的调用

题目: 用java语言实现两个函数encode()和decode(),分别实现对字符串的变换和复原.变换函数encode()顺序考察已知字符串的字符,按以下规则逐组生成新字符串: (1)若已知字符串的当前字符不是大于0的数字字符,则复制该字符于新字符串中: (2)若已知字符串的当前字符是一个数字字符,且它之后没有后继字符,则简单地将它复制到新字符串中: (3)若已知字符串的当前字符是一个大于0的数字字符,并且还有后继字符,设该数字字符的面值为n,则将它的后继字符(包括后继字符是一个数字字符)重复

java Base64 [ Encode And Decode In Base64 Using Java ]

http://www.javatips.net/blog/2011/08/how-to-encode-and-decode-in-base64-using-java http://commons.apache.org/proper/commons-codec/ 官方下载链接 Encode And Decode In Base64 Using Java explains about different techniques for Encode Base64 using java / Decode