Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions 127-word-ladder/memo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
### step1

BFSでbeginWordから1文字ずつ変えながら探索する方法で実装。

### step2

qの名前をwordToCountに変更。

### step3

3回通すまで書き直し。
32 changes: 32 additions & 0 deletions 127-word-ladder/step1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Solution {
public:
int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
unordered_set<string> wordSet(wordList.begin(), wordList.end());

if (!wordSet.count(endWord)) {
return 0;
}

queue<pair<string, int>> q;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

q はキューを表しているのでしょうか。

変数を型名にしても、読み手にとって得られる情報はあまりないように思いました。変数名には、どのような値が格納されているかが分かる名前を付けると、より読みやすくなると思います。

また、個人的には、コンテナの変数名は、複数形を表す s で終えるようにしています。 word_and_lengths や word_and_steps はいかがでしょうか?

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.

そうですね、word_and_lengths や word_and_stepsの方がよさそうです。

q.push({ beginWord, 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.

std::queue::emplace() を使用すると、要素の型のコンストラクターの引数を直接指定して、要素を追加することができます。
https://cpprefjp.github.io/reference/queue/queue/emplace.html

q.emplace(beginWord, 1);

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.

そうですね、emplaceの方がすっきり書けてよさそうです。


while (!q.empty()) {
auto [word, steps] = q.front();
q.pop();
for (int i = 0; i < word.size(); i++) {
string temp = word;
for (char c = 'a'; c <= 'z'; c++) {
temp[i] = c;
if (temp == endWord) {
return steps + 1;
}
if (wordSet.count(temp)) {
q.push({ temp, steps + 1 });
wordSet.erase(temp);
}
}
}
}
return 0;
}
};
32 changes: 32 additions & 0 deletions 127-word-ladder/step2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Solution {
public:
int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
unordered_set<string> wordSet(wordList.begin(), wordList.end());

if (!wordSet.count(endWord)) {
return 0;
}

queue<pair<string, int>> wordToCount;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

To は、 map や unordered_map の場合に使う印象があります。また、 wordToCount だと、単語の出現回数というニュアンスを感じます。

wordToCount.push({ beginWord, 1 });

while (!wordToCount.empty()) {
auto [word, steps] = wordToCount.front();
wordToCount.pop();
for (int i = 0; i < word.size(); i++) {
string temp = word;
for (char c = 'a'; c <= 'z'; c++) {
temp[i] = c;
if (temp == endWord) {
return steps + 1;
}
if (wordSet.count(temp)) {
wordToCount.push({ temp, steps + 1 });
wordSet.erase(temp);
}
}
}
}
return 0;
}
};
32 changes: 32 additions & 0 deletions 127-word-ladder/step3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Solution {
public:
int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
unordered_set<string> wordSet(wordList.begin(), wordList.end());

if (!wordSet.count(endWord)) {
return 0;
}

queue<pair<string, int>> wordToCount;
wordToCount.push({ beginWord, 1 });

while (!wordToCount.empty()) {
auto [word, steps] = wordToCount.front();
wordToCount.pop();
for (int i = 0; i < word.size(); i++) {
string temp = word;
for (char c = 'a'; c <= 'z'; c++) {
temp[i] = c;
if (temp == endWord) {
return steps + 1;
}
if (wordSet.count(temp)) {
wordToCount.push({ temp, steps + 1 });
wordSet.erase(temp);
}
}
}
}
return 0;
}
};