-
Notifications
You must be signed in to change notification settings - Fork 0
127. Word Ladder #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
127. Word Ladder #19
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| ### step1 | ||
|
|
||
| BFSでbeginWordから1文字ずつ変えながら探索する方法で実装。 | ||
|
|
||
| ### step2 | ||
|
|
||
| qの名前をwordToCountに変更。 | ||
|
|
||
| ### step3 | ||
|
|
||
| 3回通すまで書き直し。 |
| 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; | ||
| q.push({ beginWord, 1 }); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. std::queue::emplace() を使用すると、要素の型のコンストラクターの引数を直接指定して、要素を追加することができます。 q.emplace(beginWord, 1);
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| } | ||
| }; | ||
| 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; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| } | ||
| }; | ||
| 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; | ||
| } | ||
| }; |
There was a problem hiding this comment.
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 はいかがでしょうか?
There was a problem hiding this comment.
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の方がよさそうです。