929.unique email addresses#14
Conversation
| 2.'+'のみ先に出現した場合(local) | ||
| 3.'@'のみ出現した場合(domain) | ||
| 4.'+'と'@'がどちらも出現した場合(domain) | ||
| で場合分けした。3と4の場合はdomain部なので何もせずに文字列を書き写せばよい。 |
There was a problem hiding this comment.
@の以前, 以後(localかdomainか)でコード中のif分も分かれていた方が読みやすいと思いました
There was a problem hiding this comment.
レビューありがとうございます。
そうですね、処理の共通性を優先しました。
There was a problem hiding this comment.
@liquo-rice
レビューありがとうございます。
なるほど、'@'の前後でforを二回に分けることで'@'のフラグは消えますね。
| at = true; | ||
| } | ||
| if ((at == false && email[i] == '.') || | ||
| (at == false && plus == true)) { |
| if (email[i] == '@') { | ||
| at = true; | ||
| } | ||
| if ((at == false && email[i] == '.') || |
There was a problem hiding this comment.
at == false という書き方は、あまり見ないように思います。 !at と書くことが多いと思います。
| public: | ||
| int numUniqueEmails(vector<string>& emails) { | ||
| vector<string> canonicalized_emails = Canonicalize(emails); | ||
| set<string> unique_emails = |
There was a problem hiding this comment.
set<string> unique_emails(canonicalized_emails.begin(), canonicalized_emails.end());のほうがシンプルだと思いました。
sort(canonicalized_emails.begin(), canonicalized_emails.end());
canonicalized_emails.erase(unique(canonicalized_emails.begin(), canonicalized_emails.end()), canonicalized_emails.end());
return canonicalized_emails.size();もありだと思います。
There was a problem hiding this comment.
ありがとうございます。
この方法ならset使わずメモリ節約できますね。
|
|
||
| private: | ||
| vector<string> Canonicalize(const vector<string>& emails) { | ||
| auto CanonicalizeEmail = [this](const string& email) { |
There was a problem hiding this comment.
C++ では、 inner function の用途でラムダ式を書くのは、あまり見ないかもしれません。イベントハンドラー等に使うのはよく見かけます。
| public: | ||
| int numUniqueEmails(vector<string>& emails) { | ||
| unordered_set<string> email_set; | ||
| for (string email : emails) { |
There was a problem hiding this comment.
こちらのコメントをご参照ください。
5ky7/arai60#22 (comment)
There was a problem hiding this comment.
レビューありがとうございます。
string型はこの場合、間接参照にしたほうがよいのですね。判断基準参考になります。
This problem: https://leetcode.com/problems/unique-email-addresses/description/
Next problem: https://leetcode.com/problems/first-unique-character-in-a-string/description/