Skip to content

50. Pow(x, n)#34

Open
ryosuketc wants to merge 1 commit into
mainfrom
50_pow_x_n
Open

50. Pow(x, n)#34
ryosuketc wants to merge 1 commit into
mainfrom
50_pow_x_n

Conversation

@ryosuketc
Copy link
Copy Markdown
Owner

Comment thread 50_pow_x_n/memo.md
* しばらく悩んでそもそもアルゴリズムがよくわかってなかったので解答を見て `Solution2AC` を書いた。
* n < 0 がありうるのを見逃していした
* n % 2 の判定を 1 回しかしていなかったり、result *= result (base *= base ではなく) していたり、そもそもアルゴリズムをちゃんと理解していなかった
* LeetCode の書き方は x, n を破壊しているんだけど、変数名も気に食わないし、base と exponent にした。
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

数値はイミュータブルなので、破壊という表現に違和感があります。

Comment thread 50_pow_x_n/step3.py
base = x
exponent = n
result = 1
while exponent != 0:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exponent > 0のほうが好みです。
なんか、ちゃんと終了してくれそうなので。

Comment thread 50_pow_x_n/step1.py
class Solution1WA:
def myPow(self, x: float, n: int) -> float:
if n == 0:
return 1
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

細かいですが、「floatを返す」としているので、1.0でも良いですね

Comment thread 50_pow_x_n/step2.py
class Solution:
def myPow(self, x: float, n: int) -> float:
if n < 0:
x = 1.0 / x
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

n<0で分母に0がくる可能性があるので、x=0の時を早期リターンしても良いかと思いました。

Comment thread 50_pow_x_n/memo.md
### step3

* 2:30
* 書いていて思ったが step2 のビットで腹落ちしていないのって、変数名の定義の他には、多分こちらの (step1 ベースの) 解答でも当てはまっていて、result を、exponent % 2 == 1 のときにしかアップデートしていないからな気がする?
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これは何も分かっていない感じがします。myMultiply にして *= を += に置き換えたら分かりますか?

@ryosuketc ryosuketc added the need re-review Review comments are lightly examined and re-review of the problem is needed. label Jul 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

need re-review Review comments are lightly examined and re-review of the problem is needed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants