获取一个数组,判断数字是否更接近 right/left 零

Get an array, decide if the number is closer to right/left zero

我正在尝试编写一个获取整数数组的代码,检查每个索引的值,如果索引的值为零,它会继续下去,直到找到一个值不为零的索引,然后更改它的值取决于数组中更接近的零。例如:

  BEFORE  ( 0, 1 , 1 , 1 , 1 , 0 , 1 , 1 , 1 , 1 , 1 , 0) 

  AFTER ( 0, 1 , 2 , 2 , 1 , 0 , 1 , 2 , 3 , 2 , 1 , 0) 

这是我的代码:

       public static void zeroDistance (int [] a)
   {
       int counterToZero = 0;
       int counterToZeroII = 0;
       for ( int i = 0; i < a.length; i++ ){
                counterToZero = 0;
                counterToZeroII = -1;
                if ( a[i] != 0 ){
                  for ( int j = i; a[j] != 0; j++)
                  { // From i to the next zero
                          counterToZero++;
                  }
                  for ( int h = i; a[h] != 0 ; h--) // From i to first zero (goes beckwards)
                  {
                        counterToZeroII++;
                  }
                  if ( counterToZero > counterToZeroII ) 
                  a[i] = counterToZero;
                  else if (counterToZero < counterToZeroII)
                  a[i] = counterToZeroII;
                  else if ( counterToZero == counterToZeroII)
                  a[i] = counterToZero;
                }
       }
       int[] b = a;
       for (int h = 0; h < b.length; h++)
        System.out.println(b[h] + "/n");
   }

这是我得到的输出,我有点不知道问题出在哪里,我仍在查看代码并尝试检测我做错了什么,如果你能帮助我的话我可能错了的地方会很有帮助。另外,如果你知道 "sort checking" 的另一种方式,比如使用递归,我将不胜感激,我正在尝试习惯使用递归,但我仍然很难实现它,因此我在以下位置使用 for 循环那一刻。

0/n 2/n 1/n 0/n 5/n 4/n 3/n 3/n 4/n 0/n

我可以向您推荐一个简单的方法。你首先从左到右,只考虑最接近的左边零。然后你从右到左,只考虑最接近右边的零。如果距离更小,则用新距离替换以前的距离。

这是它的实现方式:

public static void zeroDistance (int [] a) {
    // closest == -1 means no zero was found yet
    int closest = -1;
    for (int i=0 ; i<a.length ; i++) 
       if (a[i] == 0) 
           closest = 0;
       else {         
           // short version
           // a[i] = closest == -1 ? Integer.MAX_VALUE : ++closest;

           // simpler version for your to understand
           if (closest == -1) 
               a[i] = Integer.MAX_VALUE;
           else {
               closest++;
               a[i] = closest;
           }                    
       }
    closest = -1;
    for (int i=a.length-1 ; i>=0 ; i--) 
       if (a[i] == 0)                            
           closest = 0;
       else if (closest != -1 && a[i] > ++closest) 
           a[i] = closest;       
}

您的示例输出:

0, 1, 2, 2, 1, 0, 1, 2, 3, 2, 1, 0

这是一个使用递归的简单解决方案

public static void zeroDistance(int a[]){
    for(int x=0;x<a.length;x++){
        if(a[x]!=0){
            int leftdist=recurse(-1,a,0,x);
            int rightdist=recurse(1,a,0,x);
            int bestdist;
            if(leftdist<0||rightdist<0)
                bestdist=Math.max(leftdist,rightdist);//one (or (hopefully not) both of them) is negative(nothing awaits thou)
            else
                bestdist=Math.min(leftdist,rightdist);//both are positive, get the better one
            a[x]=bestdist;
        }
    }
}
public static int recurse(int dir,int a[],int count,int index){
    if(a[index]==0)//if we found a zero give back the count
        return count;
    if(index==0||index==a.length-1)//if we reached the end without getting a zero give negative one
        return -1;
    return recurse(dir,a,count+1,index+dir);//keep going

}