【问题标题】:Remove Leading Zeros删除前导零
【发布时间】:2025-12-03 02:20:05
【问题描述】:

我想弄清楚如何从数组中删除前导零。该数组当前存储在第一个位置的最低有效位。

示例:如果数字是 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; 

    }

【问题讨论】:

  • byte[] 转换为 ArrayList 并向后迭代,从最后一个元素开始,删除所有零。一旦找到“1”就打破循环
  • 我正在尝试只使用数组而不是转换它来做到这一点

标签: java arrays


【解决方案1】:

您的问题是输出数组与输入数组的大小相同...您跳过的0s 仍然存在,因为0 是@987654323 中任何项目的默认值@。

所以,我将从输入数组的末尾开始,直到您点击第一个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】:

    您的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); 
    }
    

    【讨论】:

    • 这是在左边计算零,并且OP已经声明前导零在右边。
    【解决方案3】:

    迭代backwards 数组并找到最后出现的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
        }
    }
    

    【讨论】:

      【解决方案4】:

      我建议 count 从最后一个连续零的数量,然后生成 output 数组,该数组的 count 大小小于原始大小...

      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 &gt;= 0; i--)
      • 是的。感谢您的关注。已编辑。
      • @Mr.Robot 快速问题:假设我的输出为 0。如果输出只是零,我确实想显示 - 我如何为此创建测试?例如,如果数字是 00。我只想显示 0-我该如何测试?
      • 为此,您必须在第一个 for loop 结束后添加一个额外的条件 if(count==arr.length) 然后创建一个大小为 1 的 byte[] output 并将其值分配给 zero 或 else byte[] output = new byte[blahblah] for(blahblah) { output[i] = copy[i]; }