-
Notifications
You must be signed in to change notification settings - Fork 0
3. Longest Substring Without Repeating Characters #48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
|
||
| ## 最終コード | ||
|
|
||
| using_characters.size()をintにキャストせずにそのままmaxに渡すと、max関数にintとsize_t(leetcodeの環境ではunsigned longみたいです)が渡されることになって(少なくともleetcodeの環境上では)エラーになってしまいました。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
テンプレート引数の型推論に失敗しているので max のようにテンプレート引数を明示してやる手もありますね。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
なるほど、max<int>(max_length, using_characters.size())という感じでしょうか。そちらの方が良いかもしれません。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
上の投稿、HTML タグとみなされて、<int> が消えました。
| class Solution { | ||
| public: | ||
| int lengthOfLongestSubstring(string s) { | ||
| unordered_set<char> using_characters; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
used_characters の方が (英語として) 自然でしょうか。現在使っている文字、というニュアンスを強く出すなら characters_in_use とかですが、*_in_use という変数名はあまり見かけないような気もします。
huyfififi
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
良いと思います 👍
| int substring_start = 0; | ||
| int max_substring_length = 0; | ||
| unordered_map<char, int> character_to_index; | ||
| for (int i = 0; i < s.size(); ++i) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
最終的なコードで使用されているendだと、ここのiの意味を考える手間が省けて嬉しいですよね。
| ++start; | ||
| } | ||
| using_characters.emplace(s[end]); | ||
| max_length = max(max_length, (int)using_characters.size()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
個人的には index の引き算をして文字列の長さを求めた方が自然な気がしました。
https://leetcode.com/problems/longest-substring-without-repeating-characters/description/