Conversation
Shunii85
reviewed
Jul 2, 2026
| 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みたいに自分は名付けました。
他は良いと思いました。
| 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()).
nodchip
reviewed
Jul 3, 2026
| 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 は省略してよいと思います。
| 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 を消せてシンプルになると思います。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
https://leetcode.com/problems/word-break/description/