-
Notifications
You must be signed in to change notification settings - Fork 0
Create 50. Pow(x, n).md #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| ``` | ||
| 負の数の割り算と余り、言語によって振る舞いが色々なので自分の使い慣れたものについては覚えておきましょう。 | ||
| ``` | ||
|
|
||
| ## 計算式による解法 | ||
| --- | ||
| 時間計算量: O(1)<br> | ||
| 空間計算量: O(1)<br> | ||
| ```python | ||
| class Solution: | ||
| def myPow(self, x: float, n: int) -> float: | ||
| return float(x ** n) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. これfloatキャストしているのは何ででしょう。 |
||
| ``` | ||
|
|
||
| ## 再帰による解法 (maximum recursion depth exceeded) | ||
| --- | ||
|
|
||
| ### 1回目 | ||
| 時間計算量: O(N)<br> | ||
| 空間計算量: O(N)<br> | ||
| よく考えたら、```if n == 1: return x```は不要。 | ||
| また、n == -1 は、最初にnが正負かで値の更新を行った方が、わかりやすい。 | ||
| (2回目以降で反映。) | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def myPow(self, x: float, n: int) -> float: | ||
| if n == 0: | ||
| return 1 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 関数が float を返していますので、整数の 1 ではなく float の 1.0 を返したほうが自然だと思います。 |
||
| if n == 1: | ||
| return x | ||
| if n == -1: | ||
| return 1 / x | ||
|
|
||
| return x * self.myPow(x, n - 1) if n > 0 else 1 / x * self.myPow(x, n + 1) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 負の数だった場合、 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 認知負荷が高いと感じます。 |
||
| ``` | ||
|
|
||
| ### 2回目 | ||
| 時間計算量: O(logN)<br> | ||
| 空間計算量: O(logN)<br> | ||
|
|
||
| 分割統治法のイメージで書いたが、直感的に何をしているのかわかづらい。 | ||
| ```python | ||
| class Solution: | ||
| def myPow(self, x: float, n: int) -> float: | ||
| if n == 0: | ||
| return 1 | ||
| if n < 0: | ||
| x = 1 / x | ||
| n *= -1 | ||
|
|
||
| divided_prod = self.myPow(x, n // 2) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| if n % 2 == 0: | ||
| return divided_prod * divided_prod | ||
| else: | ||
| return divided_prod * divided_prod * x | ||
| ``` | ||
|
|
||
| ### 3回目 | ||
| 時間計算量: O(logN)<br> | ||
| 空間計算量: O(logN)<br> | ||
| ```python | ||
| class Solution: | ||
| def myPow(self, x: float, n: int) -> float: | ||
| if n == 0: | ||
| return 1 | ||
| if n < 0: | ||
| x = 1 /x | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| n *= -1 | ||
| if n % 2 == 0: | ||
| return self.myPow(x * x, n // 2) | ||
| else: | ||
| return x * self.myPow(x * x, n // 2) | ||
| ``` | ||
|
|
||
|
|
||
|
|
||
| ## Interationによる解法 | ||
| --- | ||
| 時間計算量: O(logN)<br> | ||
| 空間計算量: O(logN)<br> | ||
| ```python | ||
| class Solution: | ||
| def myPow(self, x: float, n: int) -> float: | ||
| if n < 0: | ||
| x = 1 /x | ||
| n *= -1 | ||
|
|
||
| accumulated_prod = 1 | ||
| while n > 0: | ||
| if n % 2 != 0: | ||
| accumulated_prod *= x | ||
| x *= x | ||
| n //= 2 | ||
|
|
||
| return accumulated_prod | ||
| ``` | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
x ** n はもともと float 型だと思いますので、 float() で囲わなくてよいと思います。