数组解密

时间:2015-09-12 22:25:53

标签: java encryption substring grouping

我想解码我的加密数组。我们知道这些数字是1到9的编码。如果结果是1的编码,在子字符串中我们将不会有超过9个而只有,但我们有三个结果示例:

byte[][] A  = new byte[][] { { 0, 1, 1, 1, 1, 0, 0, 0, 1 },
{ 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0 }, 
{ 0, 1, 0, 0, 0, 0, 0, 1, 0 } }

结果:43211

byte[][] C = new byte[][] { {}, {},
{ 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1 }, {},
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0 }, {}, { 1, 1 }, { 1 }, {}, { 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, {} };

结果:1​​233

byte[][] A = new byte[][] {
{ 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1 },
{ 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 } };

结果:332132

提前致谢!

1 个答案:

答案 0 :(得分:0)

byte[][] A = new byte[][] {
            { 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1 },
            { 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1 },
            { 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 } };
String result = "";
int counter = 0;
for (byte[] arr : A) {
    for (byte b : arr) {
        if (b == 1)
            counter++;
        else {
            if (counter > 0)
                result += counter;
            counter = 0;
        }
    }
}
if (counter > 0)
    result += counter;
System.out.println(result);