Skip to content

349. Intersection of Two Arrays#14

Merged
Ryotaro25 merged 3 commits intomainfrom
problem13
May 28, 2025
Merged

349. Intersection of Two Arrays#14
Ryotaro25 merged 3 commits intomainfrom
problem13

Conversation

@Ryotaro25
Copy link
Copy Markdown
Owner

問題へのリンク
https://leetcode.com/problems/intersection-of-two-arrays/description/

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

備考

次に解く問題の予告
Unique Email Addresses

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

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

Comment on lines +4 to +7
set<int> unique_number1;
for (auto i : number1) {
unique_number1.insert(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.

Suggested change
set<int> unique_number1;
for (auto i : number1) {
unique_number1.insert(i);
}
set<int> unique_number1(number1.begin(), number1.end());

こうも書けますね
https://en.cppreference.com/w/cpp/container/set/set

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.

@fhiyo
レビューありがとうございます。
この書き方知りませんでした。こちらの方がスッキリかけますね。step4.cppに追加しました。

@@ -0,0 +1,35 @@
## ステップ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.

set, unordered_set, map, unordered_mapの違いがあまりよく分かっていないような印象を受けました。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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


int i = 0;
int j = 0;
int current_numbebr = -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.

-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.

@oda
レビューありがとうございます。
number1とnumber2が同じになった場合に現在の数字をcurrent_numberに設定しました。またこのタイミングでループを追加し重複がなくなるまでインクリメントするようにしました。

@Ryotaro25 Ryotaro25 merged commit 0918837 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