17. Letter Combinations of a Phone Number - #112
Conversation
| #include <vector> | ||
|
|
||
|
|
||
| const std::unordered_map<char, std::vector<char>> DIGIT_TO_LETTERS = { |
There was a problem hiding this comment.
コーディングスタイルによっては、定数の変数名を kDigitToLetters とする場合もあります。
参考までにスタイルガイドへのリンクを共有いたします。
https://google.github.io/styleguide/cppguide.html#Constant_Names
Variables declared constexpr or const, and whose value is fixed for the duration of the program, are named with a leading "k" followed by mixed case.
なお、このスタイルガイドは“唯一の正解”というわけではなく、数あるガイドラインの一つに過ぎません。チームによって重視される書き方や慣習も異なります。そのため、ご自身の中に基準を持ちつつも、最終的にはチームの一般的な書き方に合わせることをお勧めします。
There was a problem hiding this comment.
最初にkをつけるスタイルがあるのですね。勉強になります。
| #include <vector> | ||
|
|
||
|
|
||
| const std::unordered_map<char, std::vector<char>> DIGIT_TO_LETTERS = { |
There was a problem hiding this comment.
static が付いていない場合、他の翻訳単位から extern することで、このインスタンスを参照することができます。他の翻訳単位から参照する必要がない場合は、翻訳単位内からのみ参照できるようにするため、 static を付けても良いかもしれません。
There was a problem hiding this comment.
今回はstaticをつけた方が良さそうですね。ありがとうございます。
| public: | ||
| std::vector<std::string> letterCombinations(std::string digits) { | ||
| return LetterCombinations(digits, 0); | ||
| } |
| std::vector<std::string> combinations; | ||
| std::vector<std::string> child_combinations = LetterCombinations(digits, i + 1); | ||
| for (char letter : DIGIT_TO_LETTERS.at(digits[i])) { | ||
| for (std::string child_combination : child_combinations) { |
There was a problem hiding this comment.
std::string のコピーは、ヒープの確保と解放が入るため重いです。 const std::string& で受けたほうが良いと思います。
|
|
||
|
|
||
| } | ||
|
|
https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/