Skip to content

139. Word Break#37

Open
5103246 wants to merge 1 commit intomainfrom
139-word-break
Open

139. Word Break#37
5103246 wants to merge 1 commit intomainfrom
139-word-break

Conversation

@5103246
Copy link
Copy Markdown
Owner

@5103246 5103246 commented Dec 26, 2025

Comment on lines +171 to +172
static constexpr char kFail = 0;
static constexpr char kSuccess = 1;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

個人的には、0, 1 で false, true を表すのは普通なのでこの定数はなくてもいいかなと感じます。

can_segment.front() = kSuccess;

for (int i = 1; i <= s_view.size(); ++i) {
for (auto word : word_dict) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ここ、コピーしてますがしなくてもいいですね。const auto & で。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

たしかに、そうですね。
辞書内の単語の長さは最大20なのでconst参照の方がよかったですね。


- https://github.com/katsukii/leetcode/pull/11/files
- Trieを使った解き方もある。
- 実装が大変そうだったので、後でやろう。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copy link
Copy Markdown

@naoto-iwase naoto-iwase left a comment

Choose a reason for hiding this comment

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

選択肢を持って検討されていよいと思います。

すでにコメントが付いている点以外で読んでいて気になったことはありませんでした。

class Solution {
public:
bool wordBreak(const string& s, const vector<string>& word_dict) {
vector<char> can_segment(s.size() + 1, kFalse);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

少しお行儀が悪い感じがするのですが、

vector<char> can_segment(s.size() + 1, false);

としても良いかなと思いました。

if (i + word.size() > s.size()) {
continue;
}
if (s.substr(i, word.size()) != word) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

C++26 からは std::string::subview() 関数を使うことで、簡潔に書けるようです。
https://cpprefjp.github.io/reference/string/basic_string/subview.html

if (s.substr(i, word.size()) != word) {
continue;
}
can_segment[i] = can_segment[i] || can_segment[i + word.size()];
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

can_segment[i] |= can_segment[i + word.size()];

のほうがシンプルだと思います。

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.

4 participants