Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 对数组中的数字重新排序_Java_Arrays_Sorting_Indexing - Fatal编程技术网

Java 对数组中的数字重新排序

Java 对数组中的数字重新排序,java,arrays,sorting,indexing,Java,Arrays,Sorting,Indexing,我试图解决一个leetcode问题: 给定一个数组nums,编写一个函数将所有0移到它的末尾,同时保持非零元素的相对顺序。 例子: 输入:[0,1,0,3,12] 输出:[1,3,12,0,0] 我想我有正确的解决方案,但我不知道为什么我得到的答案是错误的 class Solution { public void moveZeroes(int[] nums) { for (int i = 0; i > nums.length;i++) { i

我试图解决一个leetcode问题:

给定一个数组nums,编写一个函数将所有0移到它的末尾,同时保持非零元素的相对顺序。 例子: 输入:[0,1,0,3,12] 输出:[1,3,12,0,0]

我想我有正确的解决方案,但我不知道为什么我得到的答案是错误的

class Solution {
    public void moveZeroes(int[] nums) {
        for (int i = 0; i > nums.length;i++) {
            int j= i;
            while ((j<nums.length) && (nums[j]==0)){
                j++;
            }
            if (j<nums.length){
                nums[i]=nums[j];
                nums[j]=0;
            }
        }
        
    }
}
类解决方案{
公共无效移动零(int[]nums){
对于(int i=0;i>nums.length;i++){
int j=i;

while((jfor)循环应该是:
for(int i=0;i
然后,循环将从0开始在数组索引上运行,直到达到数组的长度


当前代码甚至不会进入循环,因为您已经定义了i=0,并且循环条件是仅当它大于数组大小时才运行循环:(i>nums.length),这当然不是真的

您可以用一个指针解决这个问题O(N)。这将通过:

public class Solution {
    public static void moveZeroes(int[] nums) {
        if (nums == null || nums.length == 0)
            return;

        int pos = 0;
        for (int num : nums)
            if (num != 0)
                nums[pos++] = num;
        while (pos < nums.length)
            nums[pos++] = 0; 
    }
}
公共类解决方案{
公共静态无效移动零(int[]nums){
if(nums==null | | nums.length==0)
返回;
int pos=0;
for(int-num:nums)
如果(num!=0)
nums[pos++]=num;
while(位置

工具书类
  • 如需更多详细信息,您可以查看。其中有大量可接受的解决方案,包括各种解释、有效算法以及渐近/复杂性分析

for循环不正确(必须是
i
),而且,如果无需执行以下操作,您的解决方案也无法工作:

    final int[] expectedArray = {1,2,0,0};
    final String expectedString = Arrays.toString(expectedArray);
    
    int[] nothingToDo = {1,2,0,0};
    moveZeroes(nothingToDo);
    assertEquals(expectedString, Arrays.toString(nothingToDo));
收益率:

org.junit.ComparisonFailure: expected:<[[1, 2], 0, 0]> but was:<[[0, 0], 0, 0]>

您正在使用
i>nums.length
这就是为什么不执行循环

你需要一个非零值索引。在我的解决方案中,
j
是非零值索引,意思是当你找到一个非零值集
j
索引并增加它。如果
j
小于
i
或等于,这意味着将找到零,然后设置它

  public void moveZeroes(int[] nums) {
    int j = 0;
    for (int i = 0; i < nums.length; i++) {
      if (nums[i] != 0) {
        nums[j] = nums[i];
        j++;
      }
      if (j <= i) {
        nums[i] = 0;
      }
    }
  }
public void movezero(int[]nums){
int j=0;
对于(int i=0;i如果(j
i>nums.length
在for循环中。这使得永远不会执行for循环。它应该是
i
尝试大小写,如
0,1,0,0,0,3,12
这是一个分区问题,您可以尝试一种通用的分区解决方案,它可以使代码更简洁,例如:结果相同,但不移动零正如任务所暗示的,您只是将非零复制到前面,然后在末尾插入0。
@Test
public void testEmptyArray() {
    int[] array = new int[0];
    moveZeroes(array);
    assertEquals(0,array.length);
}

@Test
public void testZeroOnlyArrays() {
    int[] array = {0,0,0,0};
    final String arrayString = Arrays.toString(array);
    moveZeroes(array);
    assertEquals(arrayString, Arrays.toString(array));;
}

@Test
public void mixedTest() {
    
    int[] array = {0,1,0,2};
    final int[] expectedArray = {1,2,0,0};
    final String expectedString = Arrays.toString(expectedArray);
    moveZeroes(array);
    assertEquals(expectedString, Arrays.toString(array));;
    
    int[] nothingToDo = {1,2,0,0};
    moveZeroes(nothingToDo);
    assertEquals(expectedString, Arrays.toString(nothingToDo));
}
  public void moveZeroes(int[] nums) {
    int j = 0;
    for (int i = 0; i < nums.length; i++) {
      if (nums[i] != 0) {
        nums[j] = nums[i];
        j++;
      }
      if (j <= i) {
        nums[i] = 0;
      }
    }
  }