-
Notifications
You must be signed in to change notification settings - Fork 0
125. Valid Palindrome #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?
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 |
|---|---|---|
| @@ -1 +1,160 @@ | ||
| # Step1 | ||
|
|
||
| ## アプローチ | ||
|
|
||
| * 一番最初に思いついたもの | ||
| * 先頭からまず, 英数字以外を削除 -> 前から読んだ文字列と後ろから読んだ文字列の一致を比較 | ||
| * palindromeということは, 真ん中から文字列を見ればいい | ||
| * 最初の文字列の削除もしなくていい(無視すればいい) | ||
| * ここまで 1:53 | ||
|
|
||
| ## Code1-1 | ||
|
|
||
| * AC: 22:31 | ||
| * 真ん中から左右に広げてみる方法を考えていたが, 文字列以外を無視する時に, 与えられた`s`の長さの真ん中を取るのは良くなかった | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def is_alphanumeric(self, ch: str) -> bool: | ||
| if ord("0") <= ord(ch) <= ord("9"): | ||
| return True | ||
| if ord("a") <= ord(ch) <= ord("z") or ord("A") <= ord(ch) <= ord("Z"): | ||
| return True | ||
| return False | ||
|
|
||
| def isPalindrome(self, s: str) -> bool: | ||
| if not s: | ||
| return True | ||
|
|
||
| n = len(s) | ||
|
|
||
| left = 0 | ||
| right = n - 1 | ||
| while left < right: | ||
| if not self.is_alphanumeric(s[left]): | ||
| left += 1 | ||
| continue | ||
| if not self.is_alphanumeric(s[right]): | ||
| right -= 1 | ||
| continue | ||
| if s[left].lower() == s[right].lower(): | ||
| left += 1 | ||
| right -= 1 | ||
| continue | ||
| return False | ||
|
|
||
| return True | ||
|
|
||
| ``` | ||
|
|
||
| # Step2 | ||
|
|
||
| ## Code2-1 | ||
|
|
||
| * 変更なし | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def is_alphanumeric(self, ch: str) -> bool: | ||
| if ord("0") <= ord(ch) <= ord("9"): | ||
| return True | ||
| if ord("a") <= ord(ch) <= ord("z") or ord("A") <= ord(ch) <= ord("Z"): | ||
| return True | ||
| return False | ||
|
|
||
| def isPalindrome(self, s: str) -> bool: | ||
| if not s: | ||
| return True | ||
|
|
||
| n = len(s) | ||
|
|
||
| left = 0 | ||
| right = n - 1 | ||
| while left < right: | ||
| if not self.is_alphanumeric(s[left]): | ||
| left += 1 | ||
| continue | ||
| if not self.is_alphanumeric(s[right]): | ||
| right -= 1 | ||
| continue | ||
| if s[left].lower() == s[right].lower(): | ||
| left += 1 | ||
| right -= 1 | ||
| continue | ||
| return False | ||
|
|
||
| return True | ||
|
|
||
| ``` | ||
|
|
||
| ## Code2-2 | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def is_alphanumeric(self, ch: str) -> bool: | ||
| if ord("0") <= ord(ch) <= ord("9"): | ||
| return True | ||
| if ord("a") <= ord(ch) <= ord("z") or ord("A") <= ord(ch) <= ord("Z"): | ||
| return True | ||
| return False | ||
|
|
||
| def isPalindrome(self, s: str) -> bool: | ||
| alphanumerics = [] | ||
| for ch in s: | ||
| if self.is_alphanumeric(ch): | ||
| alphanumerics.append(ch.lower()) | ||
|
|
||
| if alphanumerics == list(reversed(alphanumerics)): | ||
| return True | ||
| else: | ||
| return False | ||
|
Comment on lines
+107
to
+110
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. return alphanumerics == list(reversed(alphanumerics)) で良い気がします。 あと、list(reversed(alphanumerics)) は alphanumerics[::-1] と書くこともできますね。
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. そうですね。 の方が簡潔でよさそうです
ですね。自分は |
||
|
|
||
| ``` | ||
|
|
||
| ## 他の人のPRを見る | ||
|
|
||
| * https://github.com/TaisukeFujise/leetcode_tafujise/pull/10 | ||
| * 自分のcode2-1と同じ | ||
| * https://github.com/naoto-iwase/leetcode/pull/63 | ||
| * `isalnum`を避けたいという感覚が一緒. | ||
| * 自分は`ord`を使ったが, このPRは`re`を使っている | ||
| * https://github.com/ryosuketc/leetcode_grind75/pull/5 | ||
| * https://github.com/huyfififi/coding-challenges/pull/5 | ||
|
|
||
| # Step3 | ||
|
|
||
| ## Code3-1 | ||
|
|
||
| * 1st: 3:30 | ||
| * 2nd: 2:06 | ||
| * 3rd: 1:01 | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def is_alphanumeric(self, ch: str) -> bool: | ||
| if ord("0") <= ord(ch) <= ord("9"): | ||
| return True | ||
| if ord("a") <= ord(ch) <= ord("z") or ord("A") <= ord(ch) <= ord("Z"): | ||
| return True | ||
| return False | ||
|
|
||
| def isPalindrome(self, s: str) -> bool: | ||
| if not s: | ||
| return True | ||
|
|
||
| left = 0 | ||
| right = len(s) - 1 | ||
| while left < right: | ||
| if not self.is_alphanumeric(s[left]): | ||
| left += 1 | ||
| continue | ||
| if not self.is_alphanumeric(s[right]): | ||
| right -= 1 | ||
| continue | ||
| if s[left].lower() != s[right].lower(): | ||
| return False | ||
| left += 1 | ||
| right -= 1 | ||
|
|
||
| return 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.
ここを書かなくてもwhileがskipされてTrueが返りますが、あってもいいとも思います。