我在 Face Table List 中表示了上述模型,其中F1, F2,...F_n
是模型的面,它们的面号是列表数组的索引。每个列表元素是另一个包含 3 个顶点的数组。每个顶点是一个由 3 个整数组成的数组,表示其x,y,z
坐标。
我想用坐标找出顶点的所有相邻面(x2, y2, z2)
。我提出了这个我相信可以完成任务的代码:
List faceList; //say the faceList is the table in the picture above.
int[] targetVertex = {x2, y2, z2}; //say this is the vertex I want to find with coordinates (x2, y2, z2)
List faceIndexFoundList; //This is the result, which is a list of index of the neighbouring faces of the targetVertex
for(int i=0; i<faceList.length; i++) {
bool vertexMatched = true;
for(int j=0; j<faceList[i].length; j++) {
if(faceList[i][j][0] != targetVertex[0] && faceList[i][j][1] != targetVertex[1] && faceList[i][j][2] != targetVertex[2]) {
vertexMatched = false;
break;
}
}
if(vertexMatched == true) {
faceIndexFoundList.add(i);
}
}
有人告诉我,完成这项任务的复杂性是 O(N^2)。但是使用我拥有的代码,它看起来只有 O(N)。的长度targetVertex
为 3,因为每个多边形只有 3 个顶点。所以,第二个内循环只是一个常数。然后,我只留下了外for
循环,也就是 O(N)。
我上面的代码的复杂性是多少?我做错了什么?