Skip to content

Conversation

@potrue
Copy link
Owner

@potrue potrue commented Aug 16, 2025


## 最終コード

using_characters.size()をintにキャストせずにそのままmaxに渡すと、max関数にintとsize_t(leetcodeの環境ではunsigned longみたいです)が渡されることになって(少なくともleetcodeの環境上では)エラーになってしまいました。
Copy link

Choose a reason for hiding this comment

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

テンプレート引数の型推論に失敗しているので max のようにテンプレート引数を明示してやる手もありますね。

Copy link
Owner Author

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())という感じでしょうか。そちらの方が良いかもしれません。

Copy link

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;

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 という変数名はあまり見かけないような気もします。

Copy link

@huyfififi huyfififi left a 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) {

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());

Choose a reason for hiding this comment

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

個人的には index の引き算をして文字列の長さを求めた方が自然な気がしました。

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.

6 participants