Skip to content

3 longest substring without repeating characters#49

Open
kitano-kazuki wants to merge 3 commits into
mainfrom
3-longest-substring-without-repeating-characters
Open

3 longest substring without repeating characters#49
kitano-kazuki wants to merge 3 commits into
mainfrom
3-longest-substring-without-repeating-characters

Conversation

@kitano-kazuki
Copy link
Copy Markdown
Owner

Comment thread memo.md
return 0

left = 0
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.

0から始める方が綺麗ですかね。

class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        left = 0
        right = 0
        longest_length = 0
        chars_in_window = set()

        while right < len(s):

            while s[right] in chars_in_window:
                chars_in_window.remove(s[left])
                left += 1

            chars_in_window.add(s[right])
            right += 1
            longest_length = max(longest_length, right - left)

        return longest_length

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.

同意です。以降のsliding windowの問題では0スタートにしました

Comment thread memo.md
left = 0
right = 1
longest_length = 1
chars_in_window = set(s[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.

nit: {s[0]}とリテラルで書く方がidiomaticかもしれません。

Comment thread memo.md

chars_in_window.add(s[right])
right += 1
longest_length = max(longest_length, 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 - left == len(chars_in_window)ですね。

Comment thread code1-1.py

left = 0
right = 1
longest_length = 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.

自分なら max_length と名付けるのですが、趣味の範囲だと思います。

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants