Skip to content

Conversation

@Ryotaro25
Copy link
Owner

問題へのリンク
https://leetcode.com/problems/string-to-integer-atoi/description/

問題文(プレミアムの場合)

備考

次に解く問題の予告
ZigZag Conversion

フォルダ構成
LeetCodeの問題ごとにフォルダを作成します。
フォルダ内は、step1.cpp、step2.cpp、step3.cppとmemo.mdとなります。

memo.md内に各ステップで感じたことを追記します。

std::isdigitが存在している
https://en.cppreference.com/w/cpp/string/byte/isdigit

・converted == numeric_limits<int>::max() / 10 && digit > 7と言う条件を使ったが
Copy link

Choose a reason for hiding this comment

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

7 は、numeric_limits::max() % 10 ということですかね。
おそらく、コンパイラが定数にしてくれるので、素直にこう書いてしまったらいいでしょう。

Copy link
Owner Author

Choose a reason for hiding this comment

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

@oda
レビューありがとうございます。

7 は、numeric_limits::max() % 10 ということですかね。

はいそのつもりで書きましたが、7って何?と自分でもしっくりこなかったです。
step2及びstep3では、numeric_limits::max() % 10 としました。

・処理ごとに関数に分けるのも一つの手段か
・isdigit()を使わないのかなと思ったら理由が書かれていた
 >[0-9]以外もTrueにするやつがいくつかあったと思うので使うのを止めておい
 なるほどです
Copy link

Choose a reason for hiding this comment

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

漢字などでも True になるのは Python の isdigit の話ですね。

Copy link
Owner Author

Choose a reason for hiding this comment

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

@oda
コメントの書き方が良くなかったですね。Pythonのコードを読んで、ここの記述はPythonの話をしている前提で書いておりました🙇コメントを書き直しました。

C++であっても影響を受ける場合があるのですね。
https://cpprefjp.github.io/reference/cctype/isdigit.html

ch が数字かどうかを判定する(判定はロケールの影響を受ける)。

https://www.reddit.com/r/C_Programming/comments/som4ys/gcc_or_clang/
https://www.llvm.org/

C++を使っている方々がコンパイラに対してどれだけ理解して、何を抑えているのか知りたいです。
Copy link

Choose a reason for hiding this comment

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

自分のよく使う言語かどうかに結構よるとは思います。(よく使うと、使うときに心配になるので、常に少しずつ調査をしています。)

ざっくりとした仕組み、プリプロセッサが走って、狭義コンパイルのあとにリンクするなどは、たまに使う知識でしょう。

Copy link
Owner Author

Choose a reason for hiding this comment

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

使うときに心配になるので、常に少しずつ調査をしています。

これが身につけないといけない姿勢ですね。

ざっくりとした仕組み、プリプロセッサが走って、狭義コンパイルのあとにリンクするなどは、たまに使う知識でしょう。

この部分はしっかりと理解しておこうと思います。

int index = 0;
while (index < s.size() && s[index] == ' ') {
index++;
}

Choose a reason for hiding this comment

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

僕なら

int index = s.find_first_not_of(' ');
if (index == string::npos) return 0;

です。

index++;
}

bool is_negative = false;

Choose a reason for hiding this comment

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

int sign = 1;

もありですね
なお、

if(index >= s.size()) return 0;

してしまえば、この条件式は以下では不必要です。

Comment on lines +18 to +27
while (index < s.size() && s[index] == '0') {
index++;
}

vector<char> numeric_strs;
while (index < s.size() && isdigit(s[index])) {
numeric_strs.push_back(s[index]);
index++;
}

Choose a reason for hiding this comment

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

このロジックいらないと思いますよ
numeric_strsの代わりにsについてforを回せばよいです。僕なら、
今までのindexをfirst_indexとして、

for(int i = first_index; i < s.size(); ++i) {
    int digit = s[i] - '0';
    // ...
}

です。

if (is_negative) {
return -converted;
} else {
return converted;

Choose a reason for hiding this comment

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

is_negativeをsignにしてしまえば以下ですね。

return converted * sign;

なお、convertedよりより具体的にabs(絶対値)とかもいいんじゃないでしょうか。

Comment on lines +32 to +40
if (converted > numeric_limits<int>::max() / 10 ||
(converted == numeric_limits<int>::max() / 10 && digit > numeric_limits<int>::max() % 10)) {
if (is_negative) {
return numeric_limits<int>::min();
} else {
return numeric_limits<int>::max();
}
}
converted = converted * 10 + digit;

Choose a reason for hiding this comment

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

もしこの書き方にするなら僕はコメントが欲しいかもしれません
maxとminの値が違うのに一緒くたに扱っていいのか不安になってしまうので
max()=-min()-1なので、ifブロック内が実行されるときはmax()かmin()のどちらかしかないという内容のコメントです
でも、より率直にis_negativeによる分岐を先にやってそれぞれmaxとminと比較した方が分かりやすいと思います。

・converted == numeric_limits<int>::max() / 10 && digit > 7と言う条件を使ったが
 この7が分かりづらい気がする
・ numeric_strs[i] - '0'以外にキャストする方法は?
・longやlong long sの場合は?どうなる
Copy link

Choose a reason for hiding this comment

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

long は 64 ビットデータモデルによっては 32-bit となる点に注意しましょう。

https://ja.wikipedia.org/wiki/64%E3%83%93%E3%83%83%E3%83%88#64%E3%83%93%E3%83%83%E3%83%88%E3%83%87%E3%83%BC%E3%82%BF%E3%83%A2%E3%83%87%E3%83%AB

Copy link
Owner Author

@Ryotaro25 Ryotaro25 Apr 4, 2025

Choose a reason for hiding this comment

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

@nodchip
ありがとうございます。このコメントをいただいたのは3回目でした🙇‍♂️
意識できるようしっかり叩き込もうと思います。

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.

5 participants