Skip to content

929. Unique Email Addresses#15

Merged
Ryotaro25 merged 4 commits intomainfrom
problem14
May 28, 2025
Merged

929. Unique Email Addresses#15
Ryotaro25 merged 4 commits intomainfrom
problem14

Conversation

@Ryotaro25
Copy link
Copy Markdown
Owner

問題へのリンク
https://leetcode.com/problems/unique-email-addresses/description/

問題文(プレミアムの場合)

備考

次に解く問題の予告
387. First Unique Character in a String
https://leetcode.com/problems/first-unique-character-in-a-string/description/

フォルダ構成
LeetCodeの問題ごとにフォルダを作成します。
フォルダ内は、step1.cpp、step2.cpp、step3.cppとmemo.mdとなります。

memo.md内に各ステップで感じたことを追記します。

public:
int numUniqueEmails(vector<string>& emails) {
set<string> unique_emails;
for (auto email : emails) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

const auto& のほうがいいかしら。

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.

レビューありがとうございます。

@@ -0,0 +1,27 @@
class Solution {
private:
string canonicalize(string email) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

const string& にします?

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.

@oda レビューありがとうございます🙇 discordで指摘受けたように無駄なコピーは極力減らすように意識します。
修正版をstep4.cppとしてあげました。
d471baa

break;
}
if (email[i] != '.') {
canonicalized_email += email[i];
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

C++はあまり詳しくないので, 変なことを言っていたら流して欲しいのですが, ループ内部で文字列の結合に足し算を使うのは効率がわるかったりしないのでしょうか?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

C++ は、文字列は mutable なので特に問題はないです。

Java、Python は immutable です。ちなみに、こういうのはよく知らない言語を触るときに一番初めに確認することの一つです。

class Solution {
private:
string canonicalize(const string& email) {
int domain_start_position = email.rfind('@');
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@Ryotaro25
例えば、@がないような不正な入力が来た場合ってどうなりますか??

@Ryotaro25 Ryotaro25 merged commit 1bc91b8 into main May 28, 2025
1 check passed
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.

4 participants