50. Pow(x, n)#34
Open
ryosuketc wants to merge 1 commit into
Open
Conversation
fuga-98
reviewed
Jun 13, 2025
| * しばらく悩んでそもそもアルゴリズムがよくわかってなかったので解答を見て `Solution2AC` を書いた。 | ||
| * n < 0 がありうるのを見逃していした | ||
| * n % 2 の判定を 1 回しかしていなかったり、result *= result (base *= base ではなく) していたり、そもそもアルゴリズムをちゃんと理解していなかった | ||
| * LeetCode の書き方は x, n を破壊しているんだけど、変数名も気に食わないし、base と exponent にした。 |
fuga-98
reviewed
Jun 13, 2025
| base = x | ||
| exponent = n | ||
| result = 1 | ||
| while exponent != 0: |
There was a problem hiding this comment.
exponent > 0のほうが好みです。
なんか、ちゃんと終了してくれそうなので。
Fuminiton
reviewed
Jun 14, 2025
| class Solution1WA: | ||
| def myPow(self, x: float, n: int) -> float: | ||
| if n == 0: | ||
| return 1 |
| class Solution: | ||
| def myPow(self, x: float, n: int) -> float: | ||
| if n < 0: | ||
| x = 1.0 / x |
There was a problem hiding this comment.
n<0で分母に0がくる可能性があるので、x=0の時を早期リターンしても良いかと思いました。
oda
reviewed
Jun 16, 2025
| ### step3 | ||
|
|
||
| * 2:30 | ||
| * 書いていて思ったが step2 のビットで腹落ちしていないのって、変数名の定義の他には、多分こちらの (step1 ベースの) 解答でも当てはまっていて、result を、exponent % 2 == 1 のときにしかアップデートしていないからな気がする? |
There was a problem hiding this comment.
これは何も分かっていない感じがします。myMultiply にして *= を += に置き換えたら分かりますか?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
50. Pow(x, n)
https://leetcode.com/problems/powx-n/