-
Notifications
You must be signed in to change notification settings - Fork 0
Create 8. String to Integer (atoi).md #23
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| 解けなかったため、他参加者の答えを参考に何度か書いてみた後、自力で再現。 | ||
| ### 1回目 | ||
| 時間計算量: O(N)<br> | ||
| 空間計算量: O(N)<br> | ||
| N: len(s) | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def myAtoi(self, s: str) -> int: | ||
| s = s.lstrip() | ||
| if not s: | ||
| return 0 | ||
|
|
||
| negative_flag = False | ||
| index = 0 | ||
| if s[index] == "-" or s[index] == "+": | ||
| if s[index] == "-": | ||
| negative_flag = True | ||
| index += 1 | ||
|
|
||
| digits_str = "" | ||
| while index < len(s): | ||
| if s[index].isdigit(): | ||
| digits_str += s[index] | ||
| index += 1 | ||
| else: | ||
| break | ||
| if not digits_str: | ||
| return 0 | ||
|
|
||
| digits_int = int(digits_str) | ||
| if negative_flag: | ||
| digits_int *= -1 | ||
|
|
||
| MAX_INT = 2 ** 31 - 1 | ||
| MIN_INT = -2 ** 31 | ||
| if digits_int > MAX_INT: | ||
| return MAX_INT | ||
| elif digits_int < MIN_INT: | ||
| return MIN_INT | ||
| else: | ||
| return digits_int | ||
| ``` | ||
|
|
||
| ### 2回目 | ||
| 他参加者の回答を参考に、下記を書き換えてみる。 | ||
| - lstripを自前で実装。 | ||
| - negative_flagをsignに変更。 | ||
| - 符号の条件判定を、in ('-', '+')に変更。 | ||
| - INT_MAX, INT_MINをビット演算で実装。 | ||
| - intへの変換を自前で実装。(ordの利用) | ||
| - 実装後、各種フラッグなどの変数を上に移動。 | ||
|
|
||
| sを直接いじるより、indexを動かしていく方がわかりやすくかつsを壊さないため安心かと思った。 | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def myAtoi(self, s: str) -> int: | ||
| index = 0 | ||
| sign = 1 | ||
| MAX_INT = (1 << 31) - 1 | ||
| MIN_INT = - (1 << 31) | ||
|
|
||
| while index < len(s) and s[index].isspace(): | ||
| index += 1 | ||
| if index == len(s): | ||
| return 0 | ||
|
|
||
| if s[index] in ('-', '+'): | ||
| if s[index] == '-': | ||
| sign = -1 | ||
| index += 1 | ||
| if index == len(s): | ||
| return 0 | ||
|
|
||
| final_integer = 0 | ||
| while index < len(s) and s[index].isdigit(): | ||
| digit = ord(s[index]) - ord('0') | ||
| final_integer = 10 * final_integer + sign * digit | ||
| if final_integer > MAX_INT: | ||
| return MAX_INT | ||
| if final_integer < MIN_INT: | ||
| return MIN_INT | ||
| index += 1 | ||
|
|
||
| return final_integer | ||
| ``` | ||
|
|
||
| ### 3回目 | ||
| ```python | ||
| class Solution: | ||
| def myAtoi(self, s: str) -> int: | ||
| index = 0 | ||
| sign = 1 | ||
| MAX_INT = (1 << 31) - 1 | ||
| MIN_INT = - (1 << 31) | ||
|
|
||
| while index < len(s) and s[index].isspace(): | ||
| index += 1 | ||
| if index == len(s): | ||
| return 0 | ||
|
|
||
| if s[index] in ('+', '-'): | ||
| if s[index] == '-': | ||
| sign = -1 | ||
| index += 1 | ||
| if index == len(s): | ||
| return 0 | ||
|
|
||
| final_integer = 0 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. なんか命名がしっくりこないです。あまり
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. あまり良い変数名が思いつかなかったので、問題文からそのまま取ってきてしまいました。処理の内容踏まえると、digits_integerとかが良いでしょうかね。 |
||
| while index < len(s) and s[index].isdigit(): | ||
| digit = ord(s[index]) - ord('0') | ||
| final_integer = 10 * final_integer + sign * digit | ||
| if final_integer > MAX_INT: | ||
| return MAX_INT | ||
| if final_integer < MIN_INT: | ||
| return MIN_INT | ||
| index += 1 | ||
| return final_integer | ||
| ``` | ||
|
|
||
| ### 4回目 | ||
| ```python | ||
| class Solution: | ||
| def myAtoi(self, s: str) -> int: | ||
| index = 0 | ||
| sign = 1 | ||
| MAX_INT = (1 << 31) - 1 | ||
| MIN_INT = - (1 << 31) | ||
|
|
||
| while index < len(s) and s[index].isspace(): | ||
| index += 1 | ||
|
|
||
| if index == len(s): | ||
| return 0 | ||
|
|
||
| if s[index] in ('+', '-'): | ||
| if s[index] == '-': | ||
| sign = -1 | ||
| index += 1 | ||
|
|
||
| if index == len(s): | ||
| return 0 | ||
|
|
||
| digits_integer = 0 | ||
| while index < len(s) and s[index].isdigit(): | ||
| digit = ord(s[index]) - ord('0') | ||
| digits_integer = 10 * digits_integer + sign * digit | ||
| if digits_integer > MAX_INT: | ||
| return MAX_INT | ||
| if digits_integer < MIN_INT: | ||
| return MIN_INT | ||
| index += 1 | ||
|
|
||
| return digits_integer | ||
| ``` | ||
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とかの方が良いかなと思います。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.
ありがとうございます。flagで片付けてしまうことがあるので、覚えておきます。