Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions 3/3.md
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) {

Choose a reason for hiding this comment

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

最終的なコードで使用されているendだと、ここの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の環境上では)エラーになってしまいました。
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 という変数名はあまり見かけないような気もします。

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

Choose a reason for hiding this comment

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

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

}
return max_length;
}
};
```