Skip to content

50. Pow(x, n)#45

Open
5ky7 wants to merge 1 commit into
mainfrom
50.-Pow(x,-n)
Open

50. Pow(x, n)#45
5ky7 wants to merge 1 commit into
mainfrom
50.-Pow(x,-n)

Conversation

@5ky7

@5ky7 5ky7 commented Apr 10, 2026

Copy link
Copy Markdown
Owner

@5ky7 5ky7 changed the title Step2 50. Pow(x, n) Apr 10, 2026
Comment thread memo.md
class Solution {
public:
double myPow(double x, long long n) {
if (n == 0) return 1.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.

ぶら下がり if 文はバグの原因になる場合があるため、避けたほうが無難だと思います。

Comment thread memo.md

if (n > 0) {
if (n % 2 == 0) {
double sqrt = myPow(x, n / 2);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

std::sqrt() と名前が被ってしまっている点に違和感を感じました。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

既存関数との名前衝突ですね.気をつけます.
しかも関数と変数の名前衝突なので余計ややこしいことになりそうです...

Comment thread memo.md
return 1.0;
}
if (n < 0) {
if (n == std::numeric_limits<int>::min()) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

特定の値だけ特別処理をしているのが、やや煩わしく感じました。自分なら int64_t を引数に取るヘルパー関数を定義し、そちらに渡すようにすると思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

定義域を拡張したhelper関数を用意した方がすっきりしますね.
ありがとうございます.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants