Java Leetcode移动零:我的bug在哪里?

Java Leetcode移动零:我的bug在哪里?,java,debugging,Java,Debugging,这是一个简单的问题。 我只是尝试了简单的insertionSorting方法来解决它,但是失败了 public class Solution { public void moveZeroes(int[] nums) { for(int i = 0; i < nums.length; i++) if(nums[i] == 0){ for(int j = i; j<nums.length-1; j

这是一个简单的问题。 我只是尝试了简单的insertionSorting方法来解决它,但是失败了

public class Solution {
    public void moveZeroes(int[] nums) {
         for(int i = 0; i < nums.length; i++)
             if(nums[i] == 0){
                    for(int j = i; j<nums.length-1; j++)
                    nums[j] = nums[j+1];
                nums[nums.length-1] = 0;
            }
    }
}
公共类解决方案{
公共无效移动零(int[]nums){
对于(int i=0;i对于(int j=i;j所以我检查了你的代码并重新编写了它。如果你要将零移到最后,这对你来说应该很好。干杯。
这段代码所做的是在数组上迭代,直到它到达一个零。在到达一个零时,它循环,将0一个位置反复向右移动,用它右边的值切换点,直到0移动到数组的末尾

示例:5回路循环 [0,1,0,2,3]>[1,0,0,2,3]>[1,0,0,2,3]>[1,0,2,0,3]>[1,0,2,3,0]

int[] array = new int[] {0,1,0,12,3};

for (int x = 0; x < array.length; x++) {
    if (array[x] == 0) {
        for (int y = x; y < array.length; y++) {
            if (y  != array.length-1) {

                // Store our replacement as temp
                int temp = array[y+1];

                // Move 0 to the right +1
                array[y+1] = array[y];

                // Set current position to what used to be right+1
                array[y] = temp;
            }
        }
    }
}
int[]数组=新的int[]{0,1,0,12,3};
对于(int x=0;x
我将分享leetcode针对移动零问题的javascript解决方案。它的时间复杂度为O(n)

针对多个零进行了优化 例子
我将尝试编写一个非常直观和简单的方法来解决这个问题。这个问题可以通过使用两个索引来解决,一个是读指针(rp),另一个是写指针(wp)

如果rp读取的值为0,则会将wp设置为此索引。然后rp会不断递增,直到找到非零值。如果读取,则会覆盖wp处的值,并且此过程会在开始时填充非零值

然后,我们只需要用零填充剩余的点,直到结束。下面是python中的一个简短解决方案:

class Solution:
    def moveZeroes(self, nums: List[int]) -> None:
        wp=rp=0
        while(rp<len(nums)):
            if(nums[rp]!=0):
                nums[wp]=nums[rp]
                wp+=1
            rp+=1
        for i in range(wp,len(nums)):
            nums[i]=0
类解决方案:
def moveZeroes(self,nums:List[int])->无:
wp=rp=0

而(rp请在下面的空间O(N)和时间O(1)中找到最优解

public void movezero(int[]nums){
// [0,1,0,3,12]
int j=0;
对于(int i=0;i
太好了,谢谢你的回答。我认为你的回答有点像是用泡泡排序的方式将“0”从前端移动到后端。但是,可以用插入排序的方式吗?请不要像这样转储代码-阅读问题的人仅仅看到“这是有效的代码”是没有用的。请实际解释这个问题,以及您的代码是如何解决的。@AndyTurner首次发表文章,感谢您的反馈。我试图使用IDE测试我的代码。我认为当变量“I”增加时,数组nums也在变化,nums[I]之后的元素也在变化正在向前移动,这会导致错误。就像它在外循环中以双倍速度进行遍历。但是,我还不知道如何修复它。
const snowball2 = nums => {
  for(let i = nums.length; i--;){
    if(nums[i]===0){
      nums.splice(i,1)
      nums.push(0);
    }
  }
  return nums
}
console.log(snowball1([0,0,1,0,0,3,0,12,0]));
console.log(snowball2([0,1,0,3,12]));
class Solution:
    def moveZeroes(self, nums: List[int]) -> None:
        wp=rp=0
        while(rp<len(nums)):
            if(nums[rp]!=0):
                nums[wp]=nums[rp]
                wp+=1
            rp+=1
        for i in range(wp,len(nums)):
            nums[i]=0
public void moveZeroes(int[] nums) {
    // [0,1,0,3,12]
    int j = 0;
    for (int i = 0; i < nums.length; i++) {
        if (nums[i] != 0) {
            int temp = nums[i];
            nums[i] = nums[j];
            nums[j] = temp;
            j++;
        }
    }
}