-
Notifications
You must be signed in to change notification settings - Fork 0
8. String to Integer (atoi) #64
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
base: main
Are you sure you want to change the base?
Conversation
| std::isdigitが存在している | ||
| https://en.cppreference.com/w/cpp/string/byte/isdigit | ||
|
|
||
| ・converted == numeric_limits<int>::max() / 10 && digit > 7と言う条件を使ったが |
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.
7 は、numeric_limits::max() % 10 ということですかね。
おそらく、コンパイラが定数にしてくれるので、素直にこう書いてしまったらいいでしょう。
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.
@oda
レビューありがとうございます。
7 は、numeric_limits::max() % 10 ということですかね。
はいそのつもりで書きましたが、7って何?と自分でもしっくりこなかったです。
step2及びstep3では、numeric_limits::max() % 10 としました。
| ・処理ごとに関数に分けるのも一つの手段か | ||
| ・isdigit()を使わないのかなと思ったら理由が書かれていた | ||
| >[0-9]以外もTrueにするやつがいくつかあったと思うので使うのを止めておい | ||
| なるほどです |
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.
漢字などでも True になるのは Python の isdigit の話ですね。
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.
@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++を使っている方々がコンパイラに対してどれだけ理解して、何を抑えているのか知りたいです。 |
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.
自分のよく使う言語かどうかに結構よるとは思います。(よく使うと、使うときに心配になるので、常に少しずつ調査をしています。)
ざっくりとした仕組み、プリプロセッサが走って、狭義コンパイルのあとにリンクするなどは、たまに使う知識でしょう。
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.
使うときに心配になるので、常に少しずつ調査をしています。
これが身につけないといけない姿勢ですね。
ざっくりとした仕組み、プリプロセッサが走って、狭義コンパイルのあとにリンクするなどは、たまに使う知識でしょう。
この部分はしっかりと理解しておこうと思います。
| int index = 0; | ||
| while (index < s.size() && s[index] == ' ') { | ||
| index++; | ||
| } |
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.
僕なら
int index = s.find_first_not_of(' ');
if (index == string::npos) return 0;です。
| index++; | ||
| } | ||
|
|
||
| bool is_negative = false; |
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.
int sign = 1;もありですね
なお、
if(index >= s.size()) return 0;してしまえば、この条件式は以下では不必要です。
| 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++; | ||
| } | ||
|
|
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.
このロジックいらないと思いますよ
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; |
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.
is_negativeをsignにしてしまえば以下ですね。
return converted * sign;なお、convertedよりより具体的にabs(絶対値)とかもいいんじゃないでしょうか。
| 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; |
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.
もしこの書き方にするなら僕はコメントが欲しいかもしれません
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の場合は?どうなる |
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.
long は 64 ビットデータモデルによっては 32-bit となる点に注意しましょう。
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.
@nodchip
ありがとうございます。このコメントをいただいたのは3回目でした🙇♂️
意識できるようしっかり叩き込もうと思います。
問題へのリンク
https://leetcode.com/problems/string-to-integer-atoi/description/
問題文(プレミアムの場合)
備考
次に解く問題の予告
ZigZag Conversion
フォルダ構成
LeetCodeの問題ごとにフォルダを作成します。
フォルダ内は、step1.cpp、step2.cpp、step3.cppとmemo.mdとなります。
memo.md内に各ステップで感じたことを追記します。