找出数组中重复数字之间的间隙

时间:2016-07-03 21:02:48

标签: java algorithm

我有一个带有数字列表的ArrayList。每个数字总是在0到9之间。因此ArrayList包含如下数字:

1 2 7 4 9 1 8 8 3 2 9 0 1 3 ....

我想在数字重复之前计算间隙。例如,数字1在再次重复之前具有4的间隙。然后它继续有另一个差距为6.我希望找出最多30个差距。这个清单很长。我希望存储并生成以下内容:

Number 0 gap 1 : none
Number 0 gap 2: 5 times
Number 0 gap 3: 7 times
.....
Number 0 gap 30: none
Number 1 gap 1: 5 times
Number 1 gap 2: 3 times
......
Number 9 gap 30: 2 times

我真的无法理解如何做到这一点。我试过以下但是显然无处可去。寻找一些方向,请帮助。

//arr is the ArrayList

int gapCount = 0;
int[] gaps = new int[10];

for (int i = 0; i < arr.size(); i++) {
     for (int j = 0; j < arr.size(); j++) {
        if(arr.get(i) == arr.get(j)){

        }
        gapCount++;
     }
}

3 个答案:

答案 0 :(得分:1)

您可以使用二维数组来保持差距:

int[][] gaps = new int[10][30];

您可以使用它来保持间隙的出现,如下所示:

gaps[0][0]保留Number 0 gap 1

的差距

gaps[0][1]保留Number 0 gap 2

的差距

...

gaps[1][0]保留Number 1 gap 1

的差距

等等。

代码如下所示:

int[][] gaps = new int[10][30];

for (int i = 0; i < (arr.size() - 1); i++) {

    int gapCount = 0;  // reset the gap count for the actual number

    for (int j = i + 1; j < arr.size(); j++) {

        if (arr.get(i) == arr.get(j)) {

            // we only track gaps greater than zero
            if(gapCount > 0) {  
                gaps[arr.get(i)][gapCount - 1]++; // increment the gap occurrence
            }

            break; // go to the next number in the list
        }

        gapCount++;

        // we don't want to track gaps greater than 30
        if(gapCount > 30) {
            break;
        }
    }
}

答案 1 :(得分:0)

这样的东西可以起作用,它确实是Java的基础:

    List<List<Integer>> gaps = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        gaps.add(new ArrayList<Integer>());
    }

    int lastPosition[] = new int[10];
    Arrays.fill(lastPosition, -1);

    for (int curIndex = 0; curIndex < arr.size(); curIndex++) {
        Integer curValue = arr.get(curIndex);
        if (lastPosition[curValue] != -1) {
            gaps.get(curValue).add(curIndex - lastPosition[curValue]);
        }
        lastPosition[curValue] = curIndex;
    }

    for (int i = 0; i < 10; i++) {
        if (gaps.get(i).isEmpty())
            System.out.print("Number " + i + ": none\n");
        else
            for (int index = 0; index < gaps.get(i).size(); index++)
                System.out.print("Number " + i + " gap " + index + " : " + gaps.get(i).get(index) + "\n");

    }

当然,你应该将它调整到你的30限制,但这很容易。

答案 2 :(得分:0)

还有一个答案:

public class GapFinder {

  public HashMap<Integer, HashMap<Integer, Integer>> findGaps(List<Integer> input) {
    HashMap<Integer, Integer> lastSeen = new HashMap<Integer, Integer>();
    HashMap<Integer, HashMap<Integer, Integer>> result = new HashMap<Integer, HashMap<Integer, Integer>>();
    for (int position = 0; position < 10; position++) {
        result.put(position, new HashMap<Integer, Integer>());
    }
    for (int position = 0; position < input.size(); position++) {
        int number = input.get(position);
        if (lastSeen.get(number) != null) {
            int diff = position - lastSeen.get(number);
            if (result.get(number).get(diff) == null) {
                result.get(number).put(diff, 1);
            } else {
                int previous = result.get(number).get(diff);
                int incremented = previous + 1;
                result.get(number).put(diff, incremented);
            }
        }
        lastSeen.put(number, position);
    }
    return result;
  }
}