Problem#
實作 pow(x, n)
意即 $x^n$
- 測資限制:
- $-100.0 < x < 100.0$
- $-2^{31} \le n \le 2^{31}-1$
- $-10^4 \le x^n \le 10^4$
想法#
快速冪
- 時間複雜度: $\mathcal{O}(\log{n})$
- 空間複雜度: $\mathcal{O}(1)$
AC Code#
賞析#
-
https://hackmd.io/@Zero871015/LeetCode-50
Square and multiply
- TODO
-
其實也可以寫得很簡單
- https://leetcode.com/problems/powx-n/submissions/509619721/
- 直接在 pow return 的地方判斷 n 的正負,關鍵是 n 是奇數時,$n>0$ 則乘 $x$;$n<0$ 時則乘 $\frac{1}{x}$