Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我必须从左到右计算数组中所有可能的对。我如何才能有效地做到这一点?_Java_Algorithm - Fatal编程技术网

Java 我必须从左到右计算数组中所有可能的对。我如何才能有效地做到这一点?

Java 我必须从左到右计算数组中所有可能的对。我如何才能有效地做到这一点?,java,algorithm,Java,Algorithm,//下面是我如何改进的实现 int[] numbers = { 1, 5, 23, 2, 1, 6, 3, 1, 8, 12, 3 }; int count = 0; int length = numbers.length; for(int i=0; i<length; i++){ for(int j= i+1;j<length; j++ ){ if(j!=i && numbers[i]==n

//下面是我如何改进的实现

    int[] numbers = { 1, 5, 23, 2, 1, 6, 3, 1, 8, 12, 3 };
    int count = 0;
    int length = numbers.length;


    for(int i=0; i<length; i++){
        for(int j= i+1;j<length; j++ ){
            if(j!=i && numbers[i]==numbers[j]){
                count+=1;
            }
        }
    }
int[]数字={1,5,23,2,1,6,3,1,8,12,3};
整数计数=0;
int length=number.length;
对于(int i=0;iO(n)中的溶液:

publicstaticvoidmain(字符串[]args){
int[]数字={1,5,23,2,1,6,3,1,8,12,3};
整数计数=0;
Map elements=newhashmap();
for(int i=0;i
对于
[1,1,1,1]
您期望
6
。在这种情况下,我们将查找所有对
{0,1}、{0,2}、{0,3}、{1,2}、{1,3}、{2,3}
,这与
6*(6-1)/2
相同。请注意,这与每次取2个元素的组合相同。一些代码如下:

def dupes(arr):
   numdict = dict()
   for idx in arr:
      numdict[idx] = numdict.get(idx, 0) + 1
   count = 0
   for key, val in numdict.items():
      if val > 1:
         count = count+val*(val-1)//2 # The // 2 means divide by 2 and ignore decimal part
   return count

>>> dupes([1, 1, 1, 1])
6

>>> dupes([1, 5, 23, 2, 1, 6, 3, 1, 8, 12, 3])
4

python中的dict相当于Java中的hashmap。

为什么您认为它需要改进?它不起作用吗?如果它起作用并且您想在上面发布您的问题,您可以删除
j!=i
。它不起作用在这种情况下“正确”的答案是什么?它是4吗?它的复杂性是O(n2),因此我可以如何改进我的解决方案输入:(1,1,1,1)所需输出:对的计数是6打印
count
,虽然这对我有意义,但不是OP想要的。他们想要
count
选择2;
(!count-!2)/(!2)
,所以
[1,1,1,1]
生成
4
计数,对的数目是
(4*3*2*1)/(2*1)/(2*1)
(4*3)/(2)
=
12/2
=
6
对。@AndyG对不起,我丢了一些东西,谁是OP?你能解释一下吗?:他们想要计数选择2,真的我不想understand@AndyG我的算法是6,Kapil jain说:输入:(1,1,1,1)所需输出:成对计数为6问题出在哪里?很抱歉,它生成了正确的输出。OP=原始海报(发布问题的人)
def dupes(arr):
   numdict = dict()
   for idx in arr:
      numdict[idx] = numdict.get(idx, 0) + 1
   count = 0
   for key, val in numdict.items():
      if val > 1:
         count = count+val*(val-1)//2 # The // 2 means divide by 2 and ignore decimal part
   return count

>>> dupes([1, 1, 1, 1])
6

>>> dupes([1, 5, 23, 2, 1, 6, 3, 1, 8, 12, 3])
4