将重复的数字移动到数组的末尾

时间:2013-03-26 01:16:45

标签: java methods

我需要做一个计算Yahtzee中的smallStraight的方法(小直意味着你有4个骰子增加一个......例如,1,2,3,4,6是一个小直线)。现在我试图克服障碍,当你对数组进行排序时,可能会有重复的数字 例如,如果您滚动然后排序,则可能会得到1, 2, 2, 3, 4。现在我基本上需要删除第二个2到数组的末尾。这是我的代码。注意,这显然不适用于嵌套的四个循环。我只是想知道最好的方法。

public int setSmallStraight(int[] die)
{
    if (!isSmallStraightUsed)
    {
      int counter = 0;
      boolean found = false;
      Arrays.sort(die);

      for (int i = 0; i < die.length - 1; i++)
      {
          if (counter == 3)
              found = true;

          if (die[i + 1] == die[i] + 1)
          {
              counter++;
          }
          else if (die[i + 1] == die[i])
          {
              continue;
          }
          else
          {
              counter = 0;
          }
      }

      if (found)
      {
         smallStraight = 30; 
      }
      else
      {
          smallStraight = 0;
      }
      return smallStraight;
    }
   else
        return 0;
   }

4 个答案:

答案 0 :(得分:1)

如果有一个int counter来计算数组中连续+1次增加的数量呢?这样的事情:

public boolean hasSmallStraight(int[] sortedValues) {
    int counter = 0;
    for (int i=0; i<sortedValues.length-1; i++) {
        if (counter == 3) return true;

        if (sortedValues[i+1] == sortedValues[i] + 1) {
            counter++;
        } else if (sortedValues[i+1] == sortedValues[i]) {
            continue;
        } else {
            counter = 0;
        }
    }

    return counter==3;
}

注意:这仅适用于小直道

答案 1 :(得分:1)

这对你有用:

public static void main(String[] args) {
Integer[] items = {0, 4, 2, 2, 10, 5, 5, 5, 2};
System.out.println(customSort(Arrays.asList(items)));
}

public static Collection<Integer> customSort(List<Integer> die) {
Collections.sort(die);
Stack<Integer> numbas = new Stack<Integer>();
List<Integer> dupes = new ArrayList<Integer>();
numbas.push(die.get(0));
for (int i = 1; i < die.size(); i++) {
    if (!die.get(i).equals(numbas.peek())) {
    numbas.push(die.get(i));
    } else {
    dupes.add(die.get(i));
    }
}
numbas.addAll(dupes);
return numbas;
}

这会产生输出

[0, 2, 4, 5, 10, 2, 2, 5, 5]

根据需要添加错误检查和处理。

答案 2 :(得分:0)

假设数组已排序,您可以使用如下算法。请阅读我提出的评论,希望它清楚地解释它是如何工作的。另外作为预防措施 - 这绝不是最有效的实现,所以如果您的阵列很大,请考虑性能。

// Sequentially iterate array using 2 indices: i & j
// Initially i points to 1st element, j point to 2nd element
// The assumption is there's at least 2 element in the array.
// 'end' acts as a boundary limit to which element hasn't been checked
for(int i=0,j=1,end=array.length; j<end; ) {

    // If element pointed by i & j are equal, shift element pointed
    // by j to the end. Decrement the end index so we don't test element
    // that's already shifted to the back.
    // Also in this case we don't increment i & j because after shifting we
    // want to perform the check at the same location again (j would have pointed
    // to the next element)
    if(array[i] == array[j]) {

        // This for loop shifts element at j to the back of array
        for(int k=j; k<array.length-1; k++) {
            int tmp = array[k+1];
            array[k+1] = array[k];
            array[k] = tmp;
        }

        end--;

    // if element at i and j are not equal, check the next ones
    } else {
        i++;
        j++;
    }
}

答案 3 :(得分:0)

    int[] a1 = { 1, 2, 2, 3, 4 };
    int[] a2 = new int[a1.length];
    int j = 0, k = 0;
    for (int i = 0; i < a1.length - 1; i++) {
        if (a1[i + 1] == a1[j]) {
            a2[k++] = a1[i + 1];
        } else {
            a1[++j] = a1[i + 1];
        }
    }
    System.arraycopy(a2, 0, a1, j + 1, k);
    System.out.println(Arrays.toString(a1));

输出

[1, 2, 3, 4, 2]