Skip to content

139. Word Break#51

Open
colorbox wants to merge 1 commit into
mainfrom
139
Open

139. Word Break#51
colorbox wants to merge 1 commit into
mainfrom
139

Conversation

@colorbox

Copy link
Copy Markdown
Owner

Comment thread 139/step3.cpp
class Solution {
public:
bool wordBreak(string s, vector<string>& wordDict) {
vector<uint8_t> segmented_indexes(s.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.

細かいですが、indexの複数形indicesです!

あと、indexが格納されているわけではないので、reachableみたいに自分は名付けました。

他は良いと思いました。

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.

コメントありがとうございます。

細かいですが、indexの複数形indicesです!

辞書を引くとindexesでもよさそうなため、個人的にわかりやすい方を使っていました。

あと、indexが格納されているわけではないので、reachableみたいに自分は名付けました。

これは確かにその通りです、indexではなく真偽値なので名前をもっと別のものにすべきでした 🙏

Comment thread 139/step3.cpp
continue;
}
for (const string& word : wordDict) {
if (s.compare(i, word.size(), word) == 0) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ここ気になったのですが、
i + word.size() - 1がsのインデックスの範囲を超えてしまったらどうなるんでしょう。

out of rangeが投げられるのですかね。

仕様書見た感じ大丈夫そうでした。

  1. Compares a [pos1, pos1 + count1) substring of this string to str.
    If count1 > size() - pos1, the substring is [pos1, size()).

https://en.cppreference.com/cpp/string/basic_string/compare

Comment thread 139/step1.cpp
class Solution {
public:
bool wordBreak(string s, vector<string>& wordDict) {
vector<bool> matched_index(s.size(), false);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

bool がデフォルト初期化されると false になるため、引数の false は省略してよいと思います。

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.

コメントありがとうございます、なるほど、デフォルト初期化をちゃんと把握できていませんでした。

Comment thread 139/step1.cpp
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) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

matched_index[0] = true で初期化しておくと、 i > 0 を消せてシンプルになると思います。

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.

ありがとうございます、確かにこれで例外処理を消せますね。

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.

3 participants