How do I shift elements in an array to the left by 1 if there is a zero?

半世苍凉 提交于 2020-12-26 12:18:24

问题


Ok so I have to go through an array and if there is a zero in the array then I have to shift the elements right of the zero to the left by 1.

For example, given:

[3,0,1,2]

after a call to the method I should get:

[3,1,2,0]

This is what I have so far;

public int[] shiftArray(int[] arr) {
    for (int i = 0; i < arr.length - 1; i++) {
        if (arr[i] == 0) {
            arr[i] = arr[i + 1];
        }
        arr[arr.length - 1] = 0;
    }
    return null;
}

I'm stuck on how to shift all the elements right of zero to the left by 1 and not just the one right next to the zero.


回答1:


The main thing your algorithm/procedure should focus on is this:

If the value in an index is zero and it isn't the last index, perform two operations:

  1. Move the value of the index to the right
  2. Move the number previously at the right to the left.

The steps above are very important. I see that you've neglected step 2 in your question.

Here's how I'd handle it:

    public int[] shiftArray(int[] arr){ 

      for(int i = 0; i < arr.length - 1; i ++) {
              if((arr[i] == 0) && (i != (arr.length - 1))){
                  int prev = arr[i];
                  int next = arr[i + 1];
                  arr[i] = next;
                  arr[i + 1] = prev;
              }
      }
    return arr;
  }

I hope this helps.. Merry coding!




回答2:


public int[] shiftArray(int[] arr)
{
    int shift = 0;

    for (int i = 0; i < arr.length; i++) {
        if (arr[i]==0) {
            shift++; // if you only want to handle the 1st zero, use shift = 1
        } else if (shift>0) {
            arr[i-shift] = arr[i];
        }
    }
    for (int i = arr.length - shift; i < arr.length; i++) {
        arr[i] = 0;
    }
    return null;
}



回答3:


Here are two ways to do it, one with loops and the other with streams

public static int[] ZeroRight(int[] arr) {
    int[] temp = new int[arr.length];
    int leftIndex = 0;
    int rightIndex = arr.length - 1;
    for (int i : arr) {
        if (i == 0) {
            temp[rightIndex] = i;
            rightIndex--;
        } else {
            temp[leftIndex] = i;
            leftIndex++;
        }
    }
    return temp;
}
public static int[] ZeroRightStream(int[] arr) {
    return Arrays.stream(arr)
            .boxed()
            .sorted((a, b) -> b == 0 ? -1 : 0)
            .mapToInt(i -> i)
            .toArray();
}



回答4:


You can use Arrays.stream method to iterate over the elements of this array, filter out unnecessary elements and reassemble the array of the same length as follows:

int[] arr = {3, 0, 1, 0, 2};

int length = arr.length;

System.out.println(Arrays.toString(arr)); // [3, 0, 1, 0, 2]

arr = Arrays.stream(
        Arrays.stream(arr)
                .filter(i -> i != 0)
                .boxed()
                .toArray(q -> new Integer[length]))
        .mapToInt(i -> i == null ? 0 : i)
        .toArray();

System.out.println(Arrays.toString(arr)); // [3, 1, 2, 0, 0]

This approach is best used for an array of objects rather than an array of primitives.
See: How can you reduce the indices of elements in a string array after deleting an element from it?



来源:https://stackoverflow.com/questions/49501676/how-do-i-shift-elements-in-an-array-to-the-left-by-1-if-there-is-a-zero

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!