125. Valid Palindrome - #6
Conversation
| public: | ||
| bool isPalindrome(string s) { | ||
| int i = 0; | ||
| int j = s.size() - 1; |
There was a problem hiding this comment.
i,jよりはleft.rightとしたほうが直感的でわかりやすいかと思いました。
There was a problem hiding this comment.
ご指摘ありがとうございます。
おっしゃるとおりですね、、!left, 'right`の方が適切だと思います。
| left_pointer, right_pointer = 0, len(s) - 1 | ||
|
|
||
| while left_pointer < right_pointer: | ||
| while left_pointer < right_pointer and s[left_pointer].isalnum() != True: |
There was a problem hiding this comment.
確認済みでしたらすみませんが,
PEP8だとwhileと同じ行に実行文を置くことは推奨されてないみたいですね
https://peps.python.org/pep-0008/
There was a problem hiding this comment.
参考のリンクありがとうございます。
left_pointer < right_pointer and s[left_pointer].isalnum() != True全体で条件文であるつもりだったのですが、これもこれも分けるべきという認識で合ってますかね、!
There was a problem hiding this comment.
ごめんなさい.見間違えていました.分けなくて良いかと思います
ただ条件式が長いので,もう少し短くできないかと思いました.
while left_pointer < right_pointer and not s[left_pointer].isalnum():
left_pointer += 1以下でodaさんもご指摘されてますね
| @@ -0,0 +1,16 @@ | |||
| class Solution: | |||
| def isPalindrome(self, s: str) -> bool: | |||
| i, j = 0, len(s) - 1 | |||
There was a problem hiding this comment.
colorboxさんと同様,left, rightのほうがわかりやすいかなと思いました
| i, j = 0, len(s) - 1 | ||
|
|
||
| while i < j: | ||
| while i < j and not s[i].isalnum(): |
There was a problem hiding this comment.
個人的な感想になってしまいますが,whileが連続していて読みにくく感じました.
また,5行目ののwhile文でi < jの条件が効いているので,ここに同じ条件を入れるのも読みにくい原因かもしれません.
ここはs[left or right]がアルファベットor数字でなければi, jを進めるという考えで
if not s[left].isalnum():
left += 1
continue
if not s[right].isalnum:
right -= 1
continue
のほうがわかりやすいかと思いました
以下の方法も確かめてみていただければと思います.
https://discord.com/channels/1084280443945353267/1200089668901937312/1203011372489777193
There was a problem hiding this comment.
具体的なコードまでありがとうございます。
おっしゃるとおりですね、こちらで一度コーディングしてみようと思います!
| while left_pointer < right_pointer: | ||
| while left_pointer < right_pointer and s[left_pointer].isalnum() != True: | ||
| left_pointer += 1 | ||
| while left_pointer < right_pointer and s[right_pointer].isalnum() != True: |
There was a problem hiding this comment.
ご指摘ありがとうございます。そこの違和感の部分は認知できてませんでした。
問題: https://leetcode.com/problems/valid-palindrome