javabat问题

时间:2009-12-03 04:46:15

标签: java arrays

返回一个包含与给定数组完全相同的数字的数组,但重新排列以便所有零都在数组的开头分组。非零数字的顺序无关紧要。所以{1,0,0,1}变为{0,0,1,1}。您可以修改并返回给定的数组或创建一个新数组。

zeroFront({1, 0, 0, 1}) → {0, 0, 1, 1}
zeroFront({0, 1, 1, 0, 1}) → {0, 0, 1, 1, 1}
zeroFront({1, 0}) → {0, 1}

这就是我所拥有的,但我知道这是非常错误的。

public int[] zeroFront(int[] nums) {

  for (int index = 0; index < nums.length; index ++)
  {
     int [] array1 = new int [index]; 
     if (nums [index] == 0)
     {
        array1 = nums [index]; 
     }
  }
  for (int index = 0; index < nums.length; index ++)
  {
     int [] array2 = new int [nums.length-index]; 
     if (nums [index] != 0)
     {
        array2 = nums [index]; 
     }
  }
  return array1 + array2;       
}

10 个答案:

答案 0 :(得分:1)

没有测试过,但应该有效:

public int[] zeroFront(int[] nums) {
  if (nums == null) {
    return null;
  }

  int[] result = new int[nums.length];
  int zeroesPos = 0;
  int othersPos = result.length - 1;
  for (int i = 0; i < nums.length; ++i) {
    if (nums[i] == 0) {
      result[zeroesPos] = 0;
      ++zeroesPos;
    } else {
      result[othersPos] = nums[i];
      --othersPos;
    }
  }
  return result;
}

这会通过扫描旧阵列并从前面填充零和从后面填充其他数字来填充新阵列。如果您不想返回新数组,可以“就地”执行类似的操作。您可以跟踪下一个0应该去哪里以及何时看到它,将其与zeroesPos处的数字交换。像这样:

public void zeroFrontInPlace(int[] nums) {
  if (nums == null) {
    return;
  }

  int zeroesPos = 0;
  for (int i = 0; i < nums.length; ++i) {
    if (nums[i] == 0) {
      nums[i] = nums[zeroesPos];  // move the non-zero number to position i
      nums[zeroesPos] = 0;  // move the zero toward the front
      ++zeroesPos;
    }
  }
}

请注意,您不必在创建临时值的情况下进行正常交换,因为您知道该值为0.

希望这有帮助。

答案 1 :(得分:1)

没有测试过,但这应该更简单..

public int[] zeroFront(int[] nums) {
      if (nums == null) {
        return null;
      }
       int zerosPos = 0;

      for (int i = 0; i < nums.length; ++i) {
        if (nums[i] == 0) {      //swap zerosPos with current position
            num[i]=num[zerosPos];
            num[zerosPos]=0;
          ++zerosPos;
             }
      }
      return num;
    }
希望这有助于!!

RDJ

答案 2 :(得分:1)

public int[] zeroFront(int[] nums) {
  Stack stack = new Stack();
  int[] nums2 = new int[nums.length];
  for(int i = 0; i < nums.length; i++) {
    if(nums[i] != 0) {
      stack.push(nums[i]);
    }
  }
  for(int i = 0; i < nums.length; i++) {
    if(nums[i] == 0) {
      stack.push(nums[i]);
    }
  }
  for(int i = 0; i < nums.length; i++) {
    nums2[i] = (Integer) stack.pop();
  }
  return nums2;
}

答案 3 :(得分:0)

排序方法不适用于负数,而另一种方法则不适用于某些情况(请参阅注释)。

最简单(也是最短)的方式是对数组进行排序(仅适用于非负数,感谢Tom):

Arrays.sort(nums);

另一种方法是在找到非0时交换值:

int lastNonZero = -1;
for(int i = 0; i < nums.length; i++) {
  if(nums[i] == 0 && lastNonZero!=-1) {
     int tmp = nums[i];
     nums[i] = nums[lastNonZero];
     nums[lastNonZero] = tmp;
     lastNonZero = -1;
  }
  else lastNonZero = i;
}

答案 4 :(得分:0)

对于显示的示例数据,可以简单地对数组进行排序。

如果它们不具代表性,则编写一个比较Math.abs(数字)而不是数字的比较器,并使用 进行排序。

答案 5 :(得分:0)

public int[] zeroFront(int[] nums) {
    int t = 0;
    for(int i=0 ; i< nums.length ; i++)
    {
        for(int j=i+1; j<nums.length; j++)
            if(nums[j]==0 && nums[i]!=0)
            {
                t=nums[i];
                nums[i]=nums[j];
                nums[j]=t;

            }
    }
    return nums;
}

答案 6 :(得分:0)

public int[] zeroFront(int[] nums) {
    int k=0;
    int narr[] = new int[nums.length];

    for(int i=0 ; i< nums.length ; i++)
        if(nums[i]==0)
        {
            narr[k]=nums[i];
            k=k+1;
        }

    for(int i=0 ; i< nums.length ; i++)
        if(nums[i]!=0)
        {
            narr[k]=nums[i];
            k=k+1;
        }

    return narr;
}

答案 7 :(得分:0)

     public int[] zeroFront(int[] nums) {

    for (int j = 0; j < nums.length; j++) {
        for (int i = 0; i < nums.length; i++) {

            if (i > 0 && nums[i] == 0) {

                if (nums[i - 1] != 0) {

                    int temp = nums[i];
                    nums[i] = nums[i - 1];
                    nums[i - 1] = temp;

                }

            }
            continue;

        }
    }

    return nums;

}

答案 8 :(得分:0)

这是我的答案:

public int[] zeroFront(int[] nums) {
        //Create an ArrayList to add the iterations where zero is.
        ArrayList<Integer> zeroPlaces = new ArrayList<Integer>();
        //Get the place where the zeros are and add it to the ArrayList.
        for (int i = 0; i < nums.length; i++)
        {
          if (nums[i] == 0)
          {
            zeroPlaces.add(i);
          }
        }
        //Create a return array to manipulate, set initally to nums.
        int[] ret = nums;
        //Sort nums by using the ArrayList.
        for (int i = 0; i < zeroPlaces.size(); i++)
        {
          int temp = ret[i];
          ret[i] = ret[zeroPlaces.get(i)];
          ret[zeroPlaces.get(i)] = temp;
        }
        //Finally return ret;
        return ret;
      }

答案 9 :(得分:0)

看看我的答案!

public int [] zeroFront(int [] nums){

  int fp = 0;// pointer at start
  int lp = nums.length-1; // pointer at end

  boolean isZero = false; // found zero
  boolean isOne = false; // found one


  while(fp < lp){ // until start is less than end


    if(nums[lp] == 0){
      // is zero found
      isZero = true;
    }

    if(nums[fp] != 0){ 
      // is other than zero found
      isOne = true;
    }

    if(isZero && isOne){
      // swap if both found

      int temp = nums[fp];
      nums[fp] = nums[lp];
      nums[lp] = temp;

      isZero = false;
      isOne = false;

    }

    if(isOne){
     // decrease end pointer
      lp--;
      continue;
    }

    if(isZero){
    // increase start pointer
      fp++;
      continue;
    }

   // increase start pointer and decrease end pointer
    fp++;
    lp--;

  }


  return nums;



}