-
Notifications
You must be signed in to change notification settings - Fork 0
8. String to Integer (atoi) #54
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
|
||
- マルチバイト文字が含まれている場合にどうなるか不安になった | ||
- 上記コードだとマルチバイト文字の先頭バイトが ' ', '-', '+' と一緒だとおかしなことになる | ||
- 調べたところ、マルチバイト文字は ascii と共存できるよう、先頭バイトは ascii の範囲外になるよう設計されているので、 |
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.
これは、文字コードによって違うのですが、JISコードは先頭バイトが ASCII とぶつかります。
最近は、あまり気にしなくていいのではないでしょうか。
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.
JIS コードが使われることはあまりなく、十分にユニコードが使われるようになったからですね。
問題: https://leetcode.com/problems/string-to-integer-atoi/description/ | ||
|
||
### Step 1 | ||
- なんか帳尻合わせの汚いコードが出来上がった印象 |
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.
ステートマシンはだいたい大変になります。
```Go | ||
func myAtoi(s string) int { | ||
isNegative := false | ||
isNumber := 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.
ステートマシンを作る場合、ステートを表す enum 相当のものを使う方法もあります。
以下 C++ の記述です。
class Solution {
private:
enum State {
Whitespace,
Signedness,
Conversion,
};
public:
int myAtoi(string s) {
State state = Whitespace;
int64_t absolute = 0;
int sign = 1;
int index = 0;
while (index < s.size()) {
char ch = s[index];
switch (state) {
case Whitespace:
if (ch == ' ') {
++index;
continue;
} else {
state = Signedness;
}
break;
case Signedness:
if (ch == '+') {
sign = 1;
++index;
state = Conversion;
} else if (ch == '-') {
sign = -1;
++index;
state = Conversion;
} else {
state = Conversion;
}
break;
case Conversion:
if (!isdigit(ch)) {
// HACK: Exit loop by setting index to s.size();
index = s.size();
continue;
}
absolute *= 10;
absolute += ch - '0';
++index;
if (absolute > std::numeric_limits<int>::max()) {
// HACK: Exit loop by setting index to s.size();
index = s.size();
continue;
}
break;
}
}
absolute *= sign;
absolute = std::clamp<int64_t>(absolute, std::numeric_limits<int>::min(), std::numeric_limits<int>::max());
return absolute;
}
};
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.
なるほどです。現在の状態がわかりやすいですね。
https://leetcode.com/problems/string-to-integer-atoi/description/