Java 确定我的程序的时间复杂度?
我的程序得到两个列表之间的对称差。 这是我的密码:Java 确定我的程序的时间复杂度?,java,algorithm,Java,Algorithm,我的程序得到两个列表之间的对称差。 这是我的密码: import java.util.*; /** * A class to find the symmetric difference between two lists. */ public class SymDifference { /** * Finds the symmetric difference between two sorted lists. *
import java.util.*;
/**
* A class to find the symmetric difference between two lists.
*/
public class SymDifference
{
/**
* Finds the symmetric difference between two sorted lists.
*
* @param L1 first list
* @param L2 second list
* @param Result list with the symmetric difference
*/
static <AnyType extends Comparable<? super AnyType>> void symDifference(List<AnyType> L1, List<AnyType> L2,
List<AnyType> Result)
{
//create two iterators to go through the list
ListIterator<AnyType> iterL1 = L1.listIterator();
ListIterator<AnyType> iterL2 = L2.listIterator();
//create two anytype objs
AnyType itemL1 = null;
AnyType itemL2 = null;
//gets the first two items of the lists
if (iterL1.hasNext() && iterL2.hasNext())
{
itemL1 = iterL1.next();
itemL2 = iterL2.next();
}
//use a while loop to compare elements of lists
while (itemL1 != null && itemL2 != null)
{
int compareResult = itemL1.compareTo(itemL2);
//elements are the same so go on to the next items
if (compareResult == 0)
{
//get next item for list L1
if (iterL1.hasNext())
{
itemL1 = iterL1.next();
}
else
{
itemL1 = null;
}
//get next item for list L2
if (iterL2.hasNext())
{
itemL2 = iterL2.next();
}
else
{
itemL2 = null;
}
}
// the item of L1 comes after the item of L2, add item from L2 to results
else if (compareResult < 0)
{
Result.add(itemL1);
//get next item for list L1
if (iterL1.hasNext())
{
itemL1 = iterL1.next();
}
//get next item for list L2
else
{
itemL1 = null;
}
}
// the item of L1 comes before the item of L2, add item from L1 to results
else
{
Result.add(itemL2);
//get next item for list L1
if (iterL2.hasNext())
{
itemL2 = iterL2.next();
}
//get next item for list L2
else
{
itemL2 = null;
}
}
}
//add remaining items from list L1
while (itemL1 != null)
{
Result.add(itemL1);
if (iterL1.hasNext())
{
itemL1 = iterL1.next();
}
else
{
itemL1 = null;
}
}
//add remaining items from list L2
while (itemL2 != null)
{
Result.add(itemL2);
if (iterL2.hasNext())
{
itemL2 = iterL1.next();
}
else
{
itemL2 = null;
}
}
}
//tester class
public static void main(String[] args)
{
ArrayList<Integer> a = new ArrayList<>();
a.add(1);
a.add(3);
a.add(5);
a.add(7);
a.add(9);
a.add(12);
ArrayList<Integer> a2 = new ArrayList<>();
a2.add(1);
a2.add(2);
a2.add(3);
a2.add(9);
a2.add(20);
ArrayList<Integer> results = new ArrayList<>();
symDifference(a, a2, results);
Collections.sort(results); // sort the results
System.out.println(results.toString());
}
}
import java.util.*;
/**
*用于查找两个列表之间对称差异的类。
*/
公共类符号差异
{
/**
*查找两个排序列表之间的对称差异。
*
*@param L1第一个列表
*@param L2第二个列表
*@param结果列表具有对称差异
*/
静态是您的代码是O(m+n)
其中m
是第一个列表的长度,n
是第二个列表的长度
复杂性与合并两个排序数组的复杂性相同。只是在最终结果中没有在两个列表之间添加公共数字。是的,您的代码是O(m+n)
,其中m
是第一个列表的长度,n
是第二个列表的长度
其复杂性与合并两个排序数组的复杂性相同。只是在最终结果中,您没有在两个列表之间添加公共数字。假设您只在每个列表中遍历一次,它看起来是线性的。是的。您所说的都是正确的O(n^2)
就是将列表中的每个元素与该列表中的每个其他元素进行比较。你没有这么做。如果你认为O表示法的意思是时间大致与,这可能更容易理解。如果您有一个大小为n
的列表,并且您访问每个元素一次,则时间将与n
成比例(即与列表大小成比例)。如果您必须遍历列表n
次,这与某些排序算法是成正比的,它与n^2成正比。如果您有m
个列表,并且每个列表的平均大小预计为n
,则遍历每个列表一次的时间与mn
成正比。这有帮助吗?@name但您只触摸each列表元素一次,对吗?那么这应该是线性的。假设你只沿着每个列表走一次,它看起来是线性的。是的。你说的都是对的O(n^2)
就是将列表中的每个元素与该列表中的每个其他元素进行比较。你没有这么做。如果你认为O表示法的意思是时间大致与,这可能更容易理解。如果您有一个大小为n
的列表,并且您访问每个元素一次,则时间将与n
成比例(即与列表大小成比例)。如果您必须遍历列表n
次,这与某些排序算法是成正比的,它与n^2成正比。如果您有m
个列表,并且每个列表的平均大小预计为n
,则遍历每个列表一次的时间与mn
成正比。这有帮助吗?@name但您只触摸each列表元素一次,对吗?那么这应该是线性的。所以,O(m+n)和O(n)是一样的。如果m==n
,那么O(m+n)
=O(2n)
=O(n)
。如果mn
,那么O(m+n)
=O(m)
。所以,基本上就是O(O(O)O(m+n)和O(n)是一样的。如果m==n
,那么O(m+n)
=O(2n)
=O(n)
。如果mn
,那么O(m+n)
,那么基本上就是O(O(|长列表长度)
),谢谢你的帮助!