add: Longest Substring Without Repeating Characters#23
Open
add: Longest Substring Without Repeating Characters#23
Conversation
oda
reviewed
Jul 29, 2024
| Map<Character, Integer> charNums = new HashMap<>(); | ||
| int l = 0; | ||
| int r = 0; | ||
| int max = 0; |
There was a problem hiding this comment.
Math.max と名前が衝突していますかね。あんまり衝突させないほうが行儀が良いかも知れません。
fhiyo
reviewed
Jul 30, 2024
| // Time spend: 18:12 | ||
| class Solution { | ||
| public int lengthOfLongestSubstring(String s) { | ||
| Map<Character, Integer> charNums = new HashMap<>(); |
There was a problem hiding this comment.
これはwindow内に存在する各文字の出現頻度ですかね?この変数が何を格納しているのか読み解くのに少し時間がかかったので、charToFrequencyInWindowくらい説明的でもいいかと思いました
| int r = 0; | ||
| int max = 0; | ||
|
|
||
| while (r < s.length()) { |
There was a problem hiding this comment.
分岐にかかわらずrはインクリメントされるので、for loopにした方がそのことが明確になるかもしれません
| Character rightChar = s.charAt(r); | ||
| charNums.put(rightChar, charNums.getOrDefault(rightChar, 0) + 1); | ||
| int windowSize = r - l + 1; | ||
| if (charNums.size() == windowSize) { |
There was a problem hiding this comment.
重複が発生したときない時にここのifに入るのを初見で理解するのが難しそうだと思いました
関数化するかコメントをつけて意図を明確にしたほうが読みやすいと思います
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
3. Longest Substring Without Repeating Characters