Javascript 压缩一串位(例如“00010011110001”)

Javascript 压缩一串位(例如“00010011110001”),javascript,algorithm,Javascript,Algorithm,我需要一个JavaScript过程,以这种方式压缩一串位: /* Compress bitsring by taking each substring of 3, 4, ..., 9 consecutive 1's or 0's and it by the number of such consecutive characters followed by the character. EXAMPLES: "10101000010111" --> "10101401031" "00110

我需要一个JavaScript过程,以这种方式压缩一串位:

/*
Compress bitsring by taking each substring of 3, 4, ..., 9 
consecutive 1's or 0's and it by the number of such consecutive
characters followed by the character. 
EXAMPLES:
"10101000010111" --> "10101401031"
"001100011111111111111" --> "0011309151"
*/
最好是程序优雅。我尝试过创建一个,但它变得很混乱:

curIdx = 0;
while (curIdx < bitsring.length)
{
    cnt = 1;
    while ((curIdx + cnt < bitString.length) && (cnt < 10) && (bitsring.charAt(curIdx) == bitsring.charAt(curIdx + cnt))
       ......   

    }
}
curIdx=0;
while(curIdx
是的,我知道我可能走错了方向,因为我嵌套了while循环和三重
&
条件之类的东西


有什么建议吗?

一个简单的方法是使用
.replace()

这将导致:

shrink( "10101000010111" ); //=> "10101401031"
shrink( "001100011111111111111" ); //=> "0011309151"
您可以使用:

演示:


不是javascript,但很容易转换。不确定它是否最优雅,但它应该是高效的

public void testCompress() {
        String values = "10101000010111";
        String values2 = "0011000111111111111111";
        String compressedResult = "";
        for (int i = 0; i < values.length();) {
            char letter = values.charAt(i);
            int numSeen = 0;
            for (int j = i; j < values.length(); j++) {
                if (values.charAt(j) == letter) {
                    numSeen++;
                } else {
                    break;
                }
            }
            if (numSeen > 1) {
                compressedResult += numSeen;
            }
            compressedResult += letter;
            i+= numSeen;
        }
        System.err.println(compressedResult);
    }
public void testCompress(){
字符串值=“10101000010111”;
字符串值2=“0011000111111111”;
字符串压缩结果=”;
对于(int i=0;i1){
压缩结果+=numSeen;
}
压缩结果+=字母;
i+=numSeen;
}
系统错误println(压缩结果);
}
带有


parseInt('00010011110001',2)。toString(16)
给出了
9f1
。如果你想还原这些数字,你可以预先添加前面的0,用
\uuu
分隔。这是什么类?当有11个连续位时会发生什么?或者10、101或111…你明白了。你具体说的“优雅”是什么意思?@RobertHarvey所说的“优雅”,我的意思是像一行或一些看起来不凌乱的东西你做的编辑越多,就越接近@TimWolla的答案:)是的,我注意到了。我正在与JSFIDLE中的预期输出进行比较。
string.replace(/(0{3,9}|1{3,9})/g, function (match) {
    return "" + match.length + match.charAt(0); 
});
> "001100011111111111111".replace(/(0{3,9}|1{3,9})/g, function (match) { return "" + match.length + match.charAt(0); });
'0011309151'
public void testCompress() {
        String values = "10101000010111";
        String values2 = "0011000111111111111111";
        String compressedResult = "";
        for (int i = 0; i < values.length();) {
            char letter = values.charAt(i);
            int numSeen = 0;
            for (int j = i; j < values.length(); j++) {
                if (values.charAt(j) == letter) {
                    numSeen++;
                } else {
                    break;
                }
            }
            if (numSeen > 1) {
                compressedResult += numSeen;
            }
            compressedResult += letter;
            i+= numSeen;
        }
        System.err.println(compressedResult);
    }
function compress(input) {
    return input.replace(/([01])\1{2,8}/g, function($0, $1) {
        return ($0.length + $1);
    });
}