Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.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 访谈中提出的时间复杂性问题 int[]a={3,5,4,3,2,2,1}; System.out.println(“重复元素为:”); 对于(int i=0;i_Java_Algorithm_Loops_Data Structures_Time Complexity - Fatal编程技术网

Java 访谈中提出的时间复杂性问题 int[]a={3,5,4,3,2,2,1}; System.out.println(“重复元素为:”); 对于(int i=0;i

Java 访谈中提出的时间复杂性问题 int[]a={3,5,4,3,2,2,1}; System.out.println(“重复元素为:”); 对于(int i=0;i,java,algorithm,loops,data-structures,time-complexity,Java,Algorithm,Loops,Data Structures,Time Complexity,当循环不依赖时,可以将循环重新写入为: int[] a = {3,5,4,3,2,2,1}; System.out.println("Duplicate elements are: "); for(int i=0; i<a.length; i++) { for(int j = i+1; j<a.length; j++) { if(a[i]==a[j] && i!=j){

当循环不依赖时,可以将循环重新写入为:

int[] a = {3,5,4,3,2,2,1};

        System.out.println("Duplicate elements are: ");

        for(int i=0; i<a.length; i++) {
            for(int j = i+1; j<a.length; j++) {
                if(a[i]==a[j]  && i!=j){
                    System.out.println(a[j] + " ");
                }
            }
        }

for(int i=0,j=0;iO(n)的解决方案
要求,即使用
HashSet
-一个
HashSet
将不接受重复值,因此您只需要一个循环,即如果要求允许使用声明的
HashSet

    for (int i = 0, j=0; i < n && j<n; i++;j++) {
        //do something in constant time
    }
int[]a={3,5,4,3,2,2,1};
//创建一个集合
Set=newhashset();
//创建用于存储重复项的集合
Set setDupes=new HashSet();
System.out.println(“重复元素为:”);
//循环数组一次
for(int num:a)
{ 
if(set.add(num)==false)
{ 
//您的重复元素
if(setdups.add(num)==true)
System.out.println(num+“”);
} 
}

这里应该做什么:
//在恒定时间内做点什么
?两个循环都从
0
增加到
n
。为什么还要使用内部循环?如果不知道嵌套循环中的内容,就不可能这样做。最后可能变成O(1),也可能是O(n)或者可能保持在O(n²)如果某物是,例如,用值填充矩阵,例如,
m[i][j]=i*j
那么这是不可能的。我已经更新了我问题中的代码。这完全是同一个问题。你现在可以详细说明一下吗?@RyanWilson现在检查代码的更新版本。你现在能告诉我怎么做吗?我们不知道循环是独立的。如果常数时间类似于
intArray[i][j]=0;
?@azurefrog我有时想知道这样的帖子是不是有意通过同一个用户将声誉从一个帐户添加到另一个帐户,还是同时添加到两个帐户。@RyanWilson闻起来像是tinfoil@CoderinoJavarino它闻起来有点…:P特别是因为OP似乎没有反应,也没有人发布答案e现在更新版本的代码。你现在能告诉我怎么做吗?现在检查更新版本的代码。你现在能告诉我怎么做吗?再看一遍这个问题后,我认为你是对的。OP不具体,我做了一些错误的假设。我删除了我的评论。@JimMischel没问题Jim。我想你只是在尝试而已来帮助我,教我一些东西。我对此没有问题。老实说,我在空闲时间时断时续地花了大约两天的时间,试图想出一个更好的解决方案,但不幸的是,我没能。
int[] a = {3,5,4,3,2,2,1};
// Create a set 
Set<Integer> set = new HashSet<Integer>();
//Create a set for storing duplicates
Set<Integer> setDupes = new HashSet<Integer>();

System.out.println("Duplicate elements are: ");

//loop the array once 
for (int num : a) 
{ 
     if (set.add(num) == false) 
     { 
        // your duplicate element
        if(setDupes.add(num) == true) 
           System.out.println(num + " ");
     } 
}