簡體   English   中英

刪除前導零

[英]Remove Leading Zeros

我試圖弄清楚如何從數組中刪除前導零。 該陣列當前存儲在第一個位置的最低有效位。

示例:如果number為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; 

    }

您的問題是輸出數組與輸入數組的大小相同...您跳過的0仍將存在,因為0int[]中任何項的默認值。

所以,我會從輸入數組的末尾開始,而不是實際初始化輸出數組,直到你達到第一個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;
}

向后遍歷數組並找到最后一個出現的索引1 (或者到達數組的前面)。 然后將數組復制到該索引並包含該索引。 因為您知道最終數組的索引,所以您知道返回數組的大小(size 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
    }
}

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

我建議從最后一個開始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; 
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM