Skip to content

add: Longest Substring Without Repeating Characters#23

Open
rossy0213 wants to merge 1 commit intomainfrom
leetcode-longest-substring-without-repeating-characters
Open

add: Longest Substring Without Repeating Characters#23
rossy0213 wants to merge 1 commit intomainfrom
leetcode-longest-substring-without-repeating-characters

Conversation

@rossy0213
Copy link
Copy Markdown
Owner

Map<Character, Integer> charNums = new HashMap<>();
int l = 0;
int r = 0;
int max = 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.

Math.max と名前が衝突していますかね。あんまり衝突させないほうが行儀が良いかも知れません。

// Time spend: 18:12
class Solution {
public int lengthOfLongestSubstring(String s) {
Map<Character, Integer> charNums = new HashMap<>();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

これはwindow内に存在する各文字の出現頻度ですかね?この変数が何を格納しているのか読み解くのに少し時間がかかったので、charToFrequencyInWindowくらい説明的でもいいかと思いました

int r = 0;
int max = 0;

while (r < s.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.

分岐にかかわらず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) {
Copy link
Copy Markdown

@Yoshiki-Iwasa Yoshiki-Iwasa Aug 8, 2024

Choose a reason for hiding this comment

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

重複が発生したときない時にここのifに入るのを初見で理解するのが難しそうだと思いました
関数化するかコメントをつけて意図を明確にしたほうが読みやすいと思います

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