[英]Define the Time Complexity for the algorithm (Java)
我有以下代碼片段:
public static int digPow(int n, int p) {
int powCounter = p;
int sum = 0;
char[] digits = (""+n).toCharArray();
for (char digit : digits) {
sum += Math.pow(Character.getNumericValue(digit), powCounter);
powCounter++;
}
if (sum % n == 0) return sum / n;
else return -1;
}
我不明白如何定義時間復雜度,因為,即使我有一個循環,在我看來它不僅僅是 O(n)。 或者它是?
它的 O(n),其中 n 是字符數組數字的長度。 由於循環將迭代 no 次,因此下一行是一個 if else 子句,它只會執行一次。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.