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みたいに自分は名付けました。

他は良いと思いました。

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 は省略してよいと思います。

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 を消せてシンプルになると思います。

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