Java解码RLE字符串

时间:2017-04-20 18:02:52

标签: java string

示例如果我有如下输入:

H9e3e2l5o

输出必须是:

Heeeeeeeeeeeellooooo

这是我到目前为止编写的代码:

public class RLE {
    public static String decode(String st) {

    char[] stArr = st.toCharArray();
    char lastseen = 0;
    StringBuilder sb = new StringBuilder();
    for (char s : stArr) {
        if (!Character.isDigit(s)) {
            lastseen = s;
            sb.append(s);
        } else {
            int n = Integer.parseInt(String.valueOf(s));
            for (int i = 0; i < n - 1; i++) {
                sb.append(lastseen);
            }
        }
    }
    return sb.toString();
}

结果:

'H9e3e2l5o' -> HHHHHHHHHeeeeelllllo

1 个答案:

答案 0 :(得分:0)

我假设是通用变体,这里是更正版本:

DataSourceLookup