0

我试图在由 Arrays.sort() 排序的 Java 数组中查找重复的段。我期望相同的整数在数组中形成重复的段。例如排序后的数组为:{1, 2, 3, 3, 3, 3, 5, 6, 8, 8, 8, 8, 8, 9, 9, 9, 9}

我想实现我的以下想法来查找重复的段:我想使用带有两个指针(i 和 j)的 while 循环。

1.) 让 i 从索引 0 开始,让 j 开始最后一个索引 (N-1)。2.) 在执行 j-- 时将 i 保持在索引 0 处。当 j 到达 i 的下一个索引并且没有找到段时,将 i 递增 1,并将 j 重新初始化为索引 N-1 3.) 重复步骤 1 和 2。如果找到了段,则将 i 递增到 j 的当前索引,并重新初始化j 到索引 N-1。4.) 当 i==j 时退出 while 循环。

以下是我的尝试,但不是来自我的执行。

int[] test = new int[] {1, 2, 3, 3, 3, 3, 5, 6, 8, 8, 8, 8, 8, 9, 9, 9, 9};
        int i = 0;
        int j = test.length - 1;
        int[] copySegment;

        while (j > i)
        {
            if (test[j] == test[i])
            {
                int segmentLength = j - i + 1;
                copySegment = new int[segmentLength];
                for (int k = j; k >= i; k--)
                {
                    copySegment[segmentLength--] = test[k];
                }
                for (int e : copySegment)
                {
                    System.out.print(e + " ");
                }
            }
            j--;
            i++;
        }
4

4 回答 4

4

如果您尝试在数组中查找重复变量并打印重复项,可能您应该从 i=0, j=i+1 的开头开始两个索引

Arrays.sort(array); //sorts the array elements in ascending order
int i,j,lastNum;
ArrayList<Integer> list = new ArrayList<Integer>();

for(i=0,j=i+1;i<array.length-1;i++,j++) {
  if(!list.isEmpty()) {
    lastNum = list.get(list.size()-1);
  } else {
    lastNum = array[i]-1; // so that compiler doesn't warn you about lastNum not being initialized
  }
  if(array[i]!=lastNum) {
      if(array[i]==array[j]) {
        list.add(array[i]);
      }
  } else {
    continue;
  }
}

Iterator<Integer> it = list.iterator();

while(it.hasNext()) {
    System.out.println(it.next().intValue());
}

编辑:正如@javaBeginner 指出的那样,我已经编辑了答案以给出明确的解释。但这是不必要的,因为问题清楚地表明数组是使用 Arrays.sort 排序的,但无论如何,更清晰的解释不会有任何伤害。

于 2013-09-14T04:11:21.470 回答
1

考虑到数组是预先排序的,(否则,我仍然可以Array.sort()对它们进行排序)我想出了一个更简单的方法来做你正在做的事情。

int[] array = new int[] {1, 2, 3, 3, 3, 3, 5, 6, 8, 8, 8, 8, 8, 9, 9, 9, 9};

Map<Integer, Integer> intList= new HashMap<Integer, Integer>(); 

int curCount=1;
for (int i=0; i<array.length-1; i++){
    if (array[i]==array[i+1] ){
        curCount++;
        if(i==array.length-2)
            intList.put(array[i], curCount);
    }
    else{
        intList.put(array[i], curCount);
        curCount=1;
    }

}

for (Map.Entry<Integer, Integer> entry : intList.entrySet())
{
    if(entry.getValue()<2)
        continue;
    else {
        for (int i=0; i<entry.getValue(); i++)
        System.out.println(entry.getKey());

    }
}

我已经运行并测试了这段代码。希望能帮助到你。

于 2013-09-14T05:27:47.533 回答
0

与您的解释相反,您的代码在递减时不会保持i固定。j事实上,每一次j都是递减的,i是递增的。

为了实现您的意图,您需要两个while循环 - 一个外部循环递增i,一个内部循环jtest.length下降到i+1.

但是,@BlackPanther 的方法似乎以更少的计算量提供了所需的结果,所以我更喜欢它而不是你的方法。

于 2013-09-14T04:14:41.663 回答
0

我这样做的方式需要比@BlackPanther 的解决方案更多的数组访问,但我最终使它按以下方式工作:已编辑。

int[] test = new int[] {1, 2, 3, 3, 3, 3, 5, 6, 8, 8, 8, 8, 8, 9, 9, 9, 9};
        int i = 0;
        int j = test.length - 1;
        int[] copySegment;

        while (i < test.length)
        {
            while (j > i)
            {
                if (test[j] == test[i])
                {
                    for (int k = i; k <= j; k++)
                        System.out.print(test[k] + " ");
                    System.out.print("\n");
                    i = j + 1;
                    j = test.length;
                }
                j--;
            }
            i++;
            j = test.length - 1;
        }
于 2013-09-14T04:26:37.520 回答