-
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| ## 何も見ずに解いてみる | ||
|
|
||
| ```cpp | ||
| class Solution { | ||
| public: | ||
| int lengthOfLongestSubstring(string s) { | ||
| int substring_start = 0; | ||
| int max_substring_length = 0; | ||
| unordered_map<char, int> character_to_index; | ||
| for (int i = 0; i < s.size(); ++i) { | ||
| char c = s[i]; | ||
| if (character_to_index.contains(c)) { | ||
| int current_c_pos = character_to_index[c]; | ||
| for (int j = substring_start; j <= current_c_pos; ++j) { | ||
| character_to_index.erase(s[j]); | ||
| } | ||
| substring_start = current_c_pos + 1; | ||
| } | ||
| character_to_index[c] = i; | ||
| max_substring_length = max(max_substring_length, i - substring_start + 1); | ||
| } | ||
| return max_substring_length; | ||
| } | ||
| }; | ||
| ``` | ||
|
|
||
| ## 他の人のコードを見てみる | ||
|
|
||
| https://github.com/tokuhirat/LeetCode/pull/48/files | ||
| https://github.com/huyfififi/coding-challenges/pull/29/files | ||
| https://github.com/ryosuketc/leetcode_arai60/pull/37/files | ||
| `substring_start`の更新式を`substring_start = max(substring_start, current_c_pos + 1)`のような感じにすることで辞書をわざわざ変える必要がなくなる。なるほど。 | ||
| https://github.com/Ryotaro25/leetcode_first60/pull/52/files | ||
| c++のcharは内部でASCIIコードの整数型として扱われているので、`vector<int> letter_to_count(128, 0)`みたいなものに対して`letter_to_count['a']`みたいなことができるらしい。aからzまでのASCIIコードは10進数で97から122だそう。 | ||
|
|
||
| ## 最終コード | ||
|
|
||
| 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 commentThe reason will be displayed to describe this comment to others. Learn more. テンプレート引数の型推論に失敗しているので max のようにテンプレート引数を明示してやる手もありますね。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. なるほど、 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 上の投稿、HTML タグとみなされて、 |
||
|
|
||
| ``` | ||
| 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 commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| int max_length = 0; | ||
| int start = 0; | ||
| for (int end = 0; end < s.size(); ++end) { | ||
| while (using_characters.contains(s[end])) { | ||
| using_characters.erase(s[start]); | ||
| ++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 commentThe reason will be displayed to describe this comment to others. Learn more. 個人的には index の引き算をして文字列の長さを求めた方が自然な気がしました。 |
||
| } | ||
| return max_length; | ||
| } | ||
| }; | ||
| ``` | ||
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の意味を考える手間が省けて嬉しいですよね。