Skip to content

Create longest_substring_without_.md#3

Open
nittoco wants to merge 1 commit intomainfrom
nittoco-patch-3
Open

Create longest_substring_without_.md#3
nittoco wants to merge 1 commit intomainfrom
nittoco-patch-3

Conversation

@nittoco
Copy link
Copy Markdown
Owner

@nittoco nittoco commented Apr 13, 2024

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.

length = len(s)
longest = 0
unique_char_set = set()
while right < length:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right < length:
は、
right < len(s):
のほうが、right が s を走ることが明確化するので、私はいいと思います。

```python
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
left, right = 0, 0
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

longest = 0
unique_char_set = set()
while right < length:
if s[right] in unique_char_set:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while のほうが素直な気がしますがこれも趣味ですかね。
こうすると、上の while が for right in range(len(s)): にできるんじゃないですかね。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ありがとうございます。
この部分をwhileにして、上をfor文にしたいという理由は何がありますか。
自分の感覚としては、違反せずに範囲を広げられるならrightを足す、違反するなら範囲を狭めたいのでleftを足す、という対称的な処理ができるイメージでこのコードを採用しました。

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

どちらでもいいですが、ループは仕事の引き継ぎのようなものだと思っていて、どこで引き継ぐかです。そのときに、right が動くたびに引き継ぐというのは一つ素直、というくらいの感覚です。

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

他の人のコードを見たら、たぶん、皆さん色々だと思うのです。別に、その選択肢の範囲が見えていて選んでいるならばいいです。

- スペースの処理は迷ったが、普通の文字列と同じように扱われるらしい。
- len()の書き方を忘れていた。
- while True:をleftに関するfor文にしようとしていた。left = left+1の処理をしているのでいらない。
- if(right ≥ length) ではない。最後にright = right+1をするので。
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

というより、そのようにrightを定めている(exclusive)ためですかね。

```python
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
if(s==''):
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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):
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if 文の条件式は () で囲わないほうが多いと思います。

if current_length != current_kind:

() で囲うのは複数行に分けるときなどになると思います。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants