-
Notifications
You must be signed in to change notification settings - Fork 0
8 string to integer atoi medium #27
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
| if not s: | ||
| return 0 | ||
| index = 0 | ||
| # Process whitespace |
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://docs.python.org/ja/3.13/library/stdtypes.html#str.strip
こんなのがあるみたいです。こちらも使えるかもしれません。
| # Process signedness | ||
| is_negative = False | ||
| MINUS = "-" | ||
| PLUS = "+" |
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.
SPACE, MINUS, PLUSは見れば明らかなので変数に置かなくてもいいと思いました。
|
|
||
| # apply signedness | ||
|
|
||
| num = -num if is_negative else num |
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.
細かいですが、if is_negative: num *= -1ぐらいの書き方でもいいような気がします。
| if not s: | ||
| return 0 | ||
| # process sign | ||
| 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.
今回の問題では登場しませんが、否定的なニュアンスの bool 型の変数を否定する場合、二重否定となり、読み手に取って認知負荷がやや上がってしまいます。 bool 型の変数は肯定的なニュアンスの変数にしたほうが良いかもしれません。今回の場合は is_positive となると思います。
| if not s: | ||
| return 0 | ||
| # process sign | ||
| 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.
変数名が be 動詞で始まっていると、関数名のように見える場合があります。今回の場合は negative で十分だと思います。
| INT_MIN = -2**31 | ||
| INT_MAX = 2**31 -1 | ||
| num = min(num, INT_MAX) | ||
| num = max(num, INT_MIN) |
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.
Python だと気にしなくても良いのですが、オーバーフローする状況での取り扱いも見てみると良いと思いました。
問題へのリンク
8. String to Integer (atoi)
言語
Python
自分の解法
dislikesが多いのは、LeetCodeらしからぬ細かい仕様が多いから?
step1
leading whitespace を取り除く箇所で何度かミスってしまった
s.replace(" ", "")で全ての空白を取り除いてしまっていたwhile i < len(s) and ...と先にandしておくと、iがlen(s)に達したときにs[i]でIndexErrorになる問題を防げる時間計算量:
O(len(s))空間計算量:
O(1)Tests
s=" 00321a3"-> 321s=" +00321a3"-> 321s=" -00321a3"-> -321s=""-> 0s=" "-> 0s="a"-> 0s="a1"-> 0s=" 1"-> 1s="1"-> 1s="+1"-> 1s="-1"-> -1s="0010"-> 10s="-1_000_000_000_000"-> -2**31s="1_000_000_000_000"-> 2**31-1step2
O(n)の時間計算量とO(1)の空間計算量で実装できるis_negativeをsignに変更したsignを1か-1にしておくと、最後にnum=sign*numとするだけで済む最後の変換部分のロジックを
から
と書き換えた
digitsを逆順にする必要がなくなったpowを使わなくなったnum=0から始めて、num=num*10+digitを繰り返すことで、上の位の桁から順に処理できるordを使う方法もある(ref: https://github.com/hayashi-ay/leetcode/pull/69/files)
"0" <= s[i] <= "9"のようにするのが良いかもis_digitメソッドはASCIIコード以外にも対応したメソッドなので、今回は使わなくて良いstep3
ordや、文字列の比較を忘れていたstep4 (FB)
その他
次に解く問題の予告