Conversation
| class Solution { | ||
| public: | ||
| bool wordBreak(string s, vector<string>& wordDict) { | ||
| vector<uint8_t> segmented_indexes(s.size()); |
There was a problem hiding this comment.
細かいですが、indexの複数形indicesです!
あと、indexが格納されているわけではないので、reachableみたいに自分は名付けました。
他は良いと思いました。
There was a problem hiding this comment.
コメントありがとうございます。
細かいですが、indexの複数形indicesです!
辞書を引くとindexesでもよさそうなため、個人的にわかりやすい方を使っていました。
あと、indexが格納されているわけではないので、reachableみたいに自分は名付けました。
これは確かにその通りです、indexではなく真偽値なので名前をもっと別のものにすべきでした 🙏
| continue; | ||
| } | ||
| for (const string& word : wordDict) { | ||
| if (s.compare(i, word.size(), word) == 0) { |
There was a problem hiding this comment.
ここ気になったのですが、
i + word.size() - 1がsのインデックスの範囲を超えてしまったらどうなるんでしょう。
out of rangeが投げられるのですかね。
仕様書見た感じ大丈夫そうでした。
- Compares a [pos1, pos1 + count1) substring of this string to str.
If count1 > size() - pos1, the substring is [pos1, size()).
| class Solution { | ||
| public: | ||
| bool wordBreak(string s, vector<string>& wordDict) { | ||
| vector<bool> matched_index(s.size(), false); |
There was a problem hiding this comment.
bool がデフォルト初期化されると false になるため、引数の false は省略してよいと思います。
There was a problem hiding this comment.
コメントありがとうございます、なるほど、デフォルト初期化をちゃんと把握できていませんでした。
| bool wordBreak(string s, vector<string>& wordDict) { | ||
| vector<bool> matched_index(s.size(), false); | ||
| for (int i = 0; i < s.size(); ++i) { | ||
| if (i > 0 && matched_index[i - 1] == false) { |
There was a problem hiding this comment.
matched_index[0] = true で初期化しておくと、 i > 0 を消せてシンプルになると思います。
There was a problem hiding this comment.
ありがとうございます、確かにこれで例外処理を消せますね。
https://leetcode.com/problems/word-break/description/