获取一个数组,确定数字是否更接近于右/左零

时间:2015-01-03 18:09:43

标签: java arrays

我正在尝试编写一个获取整数数组的代码,检查每个索引的值,如果索引的值为零,它会继续,直到找到一个值为非零的索引,然后根据它改变其值数组中的零点越近。例如:

  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");
   }

这是我得到的输出,我有点迷失问题所在,我仍在查看代码并试图检测我做错了什么,如果你可以帮助我并指出哪里我可能错了,它会非常有帮助。另外,如果你知道另一种“排序检查”的方式,比如使用递归,我会非常感激,我正在尝试习惯使用递归,但是我仍然很难实现它,因此我正在使用for循环时刻。

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

2 个答案:

答案 0 :(得分:2)

我可以给你一个简单的方法来做。你首先从左到右,只考虑最近的左零。然后你从右到左走,只考虑最接近的零。如果较小,请用新的距离替换之前的距离。

以下是它的实施方式:

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

答案 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

}