移除前导零

4
我正在尝试弄清楚如何从一个数组中删除前导零。该数组当前存储的最低有效位在第一个位置。
例如:如果数字是1101,则在我的数组中存储为:[1,1,0,1]。我不想翻转数组或以任何方式改变它,只需删除前导零。我已经用粗体标出需要删除的前导零。
我试图只使用数组来完成这个操作,而不进行转换。
例如:当前输出:0 1 1 0
期望输出:0 1 1
public static byte[] normal(byte[] arr)
    {
        //check if all the numbers are 0 and last is 1
        byte [] output = new byte[copy.length];
        for(int i = arr.length;i<=0;i--)
        {
            if(arr[i]==1){          
                output[i]=copy[i];
            }

        }
        return output; 

    }

1
byte[] 转换为 ArrayList 并从最后一个元素开始向后迭代,删除所有的零。一旦找到“1”,就中断循环。 - CubeJockey
我正在尝试只使用数组而不转换它来完成这个。 - jim
https://dev59.com/IW855IYBdhLWcg3wGQRM - Mister Henson
4个回答

3
你的问题在于输出数组和输入数组大小相同... 你跳过的那些 0 还是会存在,因为 int[] 中任何项目的默认值都是 0
所以我建议从输入数组的末尾开始,直到遇到第一个 1 才初始化输出数组。这样你就知道需要多大的输出数组了。
public static byte[] normal(byte[] arr) {
    byte[] output = null;
    System.out.println(arr.length);
    for (int i = arr.length-1; i >= 0; i--) {
        if (arr[i] != 0) {
            if (output == null) {
                output = new byte[i+1];
            }
            output[i] = arr[i];
        }
    }

    if (output == null) { // cover case where input is all 0s
        output = new byte[0];
    }

    return output;
}

2

反向迭代数组,查找最后一个出现的1的索引(或者到达数组前面)。然后复制数组直到包括该索引。由于你知道最终数组的索引,所以你知道返回的数组需要多大(大小为lastIndex + 1

我已经有一段时间没有写Java代码了,所以这段代码可能无法编译或运行正常。但是这段代码可能看起来像这样:

public static byte[] normal(byte[] arr)
{
    //null check arr if desired

    int indexOfLastNonZero = arr.length - 1;
    boolean foundOne = false;
    while(!foundOne && indexOfLastNonZero >= 0)
    {
        if (arr[indexOfLastNonZero] == (byte) 1)
            foundOne = true;
        else
            indexOfLastNonZero -= 1;
    }

    if (foundOne) 
    {
        byte[] output = new byte[indexOfLastNonZero + 1];
        System.arraycopy( arr, 0, output, 0, output.length );
        return output;
    }
    else 
    {
        return null; //or size 0 array if you prefer
    }
}

2

你的output数组太长了。首先,你应该数一下前导零的数量,并创建一个比arr短但有这么多元素的输出数组:

public static byte[] normal(byte[] arr)
{
    // Count the leading zeros:
    int zeroes = 0;
    while (arr[zeroes] == 0) {
        ++zeroes;
    }

    //check if all the numbers are 0 and last is 1
    byte[] output = new byte[arr.length - zeroes];
    for(int i = output.length - 1; i >= 0; ++i) {
        output[i] = arr[i - zeroes]; 
    }
    return output; 
}

或者更好的办法是使用内置的Arrays.copyOfRange方法:

public static byte[] normal(byte[] arr)
{
    // Count the leading zeros:
    int zeroes = 0;
    while (arr[zeroes] == 0) {
        ++zeroes;
    }

    //check if all the numbers are 0 and last is 1        
    return Arrays.copyOfRange(zeroes, arr.length); 
}

1
这是在左侧计算零,而 OP 已经说明前导零位于右侧。 - fps

1
我建议从最后一个零开始计算连续零的数量,然后生成“输出”数组,其大小将比原始大小小“计数”个...
public static byte[] normal(byte[] arr)
    {
        int count = 0;

        for(int i = arr.length-1;i>=0;i--)
        {

            if(arr[i]==1){          
                break;
            }
            count++;
        }

        byte [] output = new byte[copy.length-count];
        for(int i = 0;i<(copy.length-count);i++) {
            output[i] = copy[i];
        }
        return output; 
    }

这个解决方案可以,但你在第一个for循环中有一个边界问题。将其改为for (int i = arr.length - 1; i >= 0; i--) - fps
是的,谢谢您注意到了。已编辑。 - burglarhobbit
@Mr.Robot 快问:假设我的输出是0。如果输出只是零,我确实想要显示它-我该如何创建一个测试呢?例如,如果数字是00。我只想显示0-我该如何测试呢? - jim
为此,您需要在第一个“for循环”结束后添加一个额外条件,即“if(count == arr.length)”,然后创建一个大小为1的byte [] output,并将其值分配为零。或者,如果不是这样,则用“else”的“byte [] output = new byte [blahblah] for(blahblah){output [i] = copy [i]; }”来完成。 - burglarhobbit

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接