我做了一个遍历数组的函数,并打印出该数组的任何两个值,它们的总和为K。外部for循环为O(n),但如果运行时是O(Log n)或O(n)。你能帮忙吗?谢谢!!
int canMakeSum(int *array, int n, int key){
int i, j;
for(i = 0; i < n; i++){
for(j = (i+1); j < n; j++){
if(array[i]+array[j] == key){
printf("%d + %d = %d\n", array[i], array[j], key);
}
}
}
}
答案 0 :(得分:1)
由于内部循环取决于外部循环的值,因此如果不对两者进行分析,就无法找到总程序图的复杂性。内部循环的复杂度为n - i -1
。
如果要计算程序的复杂度,可以将i = 0
从i = n - 1
到T(n) = (n - 1) + (n-2) + ... + 1 + 0 = (n-1)n/2 = \Theta(n^2)
求和。因此,总复杂度为\Theta(1)
(因为内部循环中的语句具有恒定的复杂度(public class AugmentedImageNode extends AnchorNode {
private static final String TAG = "AugmentedImageNode";
// The augmented image represented by this node.
private AugmentedImage image;
// Models of the 4 corners. We use completable futures here to simplify
// the error handling and asynchronous loading. The loading is started with the
// first construction of an instance, and then used when the image is set.
private static CompletableFuture<Void> rendobject;
private ViewRenderable testViewRenderable;
//ImageView imgView;
public AugmentedImageNode(Context context) {
// Upon construction, start loading the models for the corners of the frame.
if (rendobject == null) {
ImageView imgView = imgView.inflate(context, R.layout.imgboard, null);
Picasso.get()
.load("http://scoopak.com/wp-content/uploads/2013/06/free-hd-natural-wallpapers-download-for-pc.jpg")
.into(imgView);
rendobject =
ViewRenderable.builder()
.setView(context, imgView)
.setVerticalAlignment(ViewRenderable.VerticalAlignment.BOTTOM)
.setSizer(new FixedHeightViewSizer(0.12f))
.build()
.thenAccept(renderable -> {
testViewRenderable = renderable;
//testViewRenderable = renderable;
});
/* ModelRenderable.builder()
.setSource(context, Uri.parse("models/tinker.sfb"))
.build();
*/
}
}
/**
* Called when the AugmentedImage is detected and should be rendered. A Sceneform node tree is
* created based on an Anchor created from the image. The cornerNode is then positioned based on the
* extent of the image. There is no need to worry about world coordinates since everything is
* relative to the center of the image, which is the parent node of the corner.
*/
@SuppressWarnings({"AndroidApiChecker", "FutureReturnValueIgnored"})
public void setImage(AugmentedImage image) {
this.image = image;
// If any of the models are not loaded, then recurse when all are loaded.
if (!rendobject.isDone())
{
CompletableFuture.allOf(rendobject)//, urCorner, llCorner, lrCorner)
.thenAccept((Void aVoid) -> setImage(image))
.exceptionally(
throwable -> {
Log.e(TAG, "Exception loading", throwable);
return null;
});
}
// Set the anchor based on the center of the image.
setAnchor(image.createAnchor(image.getCenterPose()));
// Make the node(s).
Vector3 localPosition = new Vector3();
Node cornerNode;
//localPosition.set(-0.5f * image.getExtentX(), 0.0f, -0.5f * image.getExtentZ());
localPosition.set(-0.0f * image.getExtentX(), 0.1f, +0.5f * image.getExtentZ());
cornerNode = new Node();
cornerNode.setParent(this);
cornerNode.setLocalPosition(localPosition);
cornerNode.setLocalRotation(Quaternion.axisAngle(new Vector3(-1f, 0, 0), 90f));
//cornerNode.setLocalScale(scaledWidth, scaledHeight, scaledWidth);
cornerNode.setRenderable(testViewRenderable);
}
}
)。
答案 1 :(得分:1)
正如其他人已经表明的那样,内循环仍然是 O(n);这是n / 2次迭代的平均值,值1到n在外循环的迭代中平均分布。
是的,您可以在 O(n log n)中解决问题。
首先,对数组进行排序;这是n log n
。现在,您有了一个线性( O(n))过程来查找所有组合。
lo = 0
hi = n-1
while lo < hi {
sum = array[lo] + array[hi]
if sum == k {
print "Success", array[lo], array[hi]
lo += 1
hi -= 1
}
else if sum < k // need to increase total
lo += 1
else // need to decrease total
hi -= 1
答案 2 :(得分:0)
尽管内循环减少了外循环中每次迭代扫描的项目数量,但仍为O(n)。总时间复杂度为O(n ^ 2)。
假设您有25000个元素的数组。在i = 0
和j = 1
的起始点,j迭代通过的元素数(最坏的情况是没有与key匹配的元素)是24999个元素。与元素总数相比,差异很小,因此就像“通过” n个元素一样。