一、计数法
int counter = 0;foreach(GameObject slot in slotList)
{if(slot.transform.childCount > 0){counter += 1;}
}return counter == 21;
计数法的复杂度为 O(n),其中n 是插槽的数量。无论插槽是否已满,都会遍历所有插槽。
二、短路法
foreach (GameObject slot in slotList){if (slot.transform.childCount == 0){return false;}}return true;
短路法的复杂度也是 O(n),但在最坏情况下(所有插槽都已满),需要遍历所有插槽。在最优情况下(第一个插槽未被占用),只需检查一个插槽。