Conversation
| length = len(s) | ||
| longest = 0 | ||
| unique_char_set = set() | ||
| while right < length: |
There was a problem hiding this comment.
right < length:
は、
right < len(s):
のほうが、right が s を走ることが明確化するので、私はいいと思います。
| ```python | ||
| class Solution: | ||
| def lengthOfLongestSubstring(self, s: str) -> int: | ||
| left, right = 0, 0 |
There was a problem hiding this comment.
| longest = 0 | ||
| unique_char_set = set() | ||
| while right < length: | ||
| if s[right] in unique_char_set: |
There was a problem hiding this comment.
while のほうが素直な気がしますがこれも趣味ですかね。
こうすると、上の while が for right in range(len(s)): にできるんじゃないですかね。
There was a problem hiding this comment.
ありがとうございます。
この部分をwhileにして、上をfor文にしたいという理由は何がありますか。
自分の感覚としては、違反せずに範囲を広げられるならrightを足す、違反するなら範囲を狭めたいのでleftを足す、という対称的な処理ができるイメージでこのコードを採用しました。
There was a problem hiding this comment.
どちらでもいいですが、ループは仕事の引き継ぎのようなものだと思っていて、どこで引き継ぐかです。そのときに、right が動くたびに引き継ぐというのは一つ素直、というくらいの感覚です。
There was a problem hiding this comment.
他の人のコードを見たら、たぶん、皆さん色々だと思うのです。別に、その選択肢の範囲が見えていて選んでいるならばいいです。
| - スペースの処理は迷ったが、普通の文字列と同じように扱われるらしい。 | ||
| - len()の書き方を忘れていた。 | ||
| - while True:をleftに関するfor文にしようとしていた。left = left+1の処理をしているのでいらない。 | ||
| - if(right ≥ length) ではない。最後にright = right+1をするので。 |
There was a problem hiding this comment.
というより、そのようにrightを定めている(exclusive)ためですかね。
| ```python | ||
| class Solution: | ||
| def lengthOfLongestSubstring(self, s: str) -> int: | ||
| if(s==''): |
There was a problem hiding this comment.
if not s:で良いと思います。
参考までにスタイルガイドへのリンクを貼ります。
https://google.github.io/styleguide/pyguide.html#2144-decision
Use the “implicit” false if possible, e.g., if foo: rather than if foo != []:. There are a few caveats that you should keep in mind though:
ただし、上記のスタイルガイドは唯一絶対のルールではなく、複数あるスタイルガイドの一つに過ぎないということを念頭に置くことをお勧めします。また、所属するチームにより何が良いとされているかは変わります。自分の中で良い書き方の基準を持ちつつ、チームの平均的な書き方で書くことをお勧めいたします。
| while True: | ||
| current_length = right - left | ||
| current_kind = len(set(s[left:right])) | ||
| if(current_length != current_kind): |
There was a problem hiding this comment.
if 文の条件式は () で囲わないほうが多いと思います。
if current_length != current_kind:() で囲うのは複数行に分けるときなどになると思います。
URL: https://leetcode.com/problems/longest-substring-without-repeating-characters/description/?envType=list&envId=xo2bgr0r
問題文
Given a string s, find the length of the longest
substring
without repeating characters.
Example 1:
Input: s = "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.
Example 2:
Input: s = "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.
Example 3:
Input: s = "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.