Skip to content

Create 349. Intersection of Two Arrays.md#25

Open
Apo-Matchbox wants to merge 1 commit intomainfrom
349.-Intersection-of-Two-Arrays
Open

Create 349. Intersection of Two Arrays.md#25
Apo-Matchbox wants to merge 1 commit intomainfrom
349.-Intersection-of-Two-Arrays

Conversation

@Apo-Matchbox
Copy link
Copy Markdown
Owner

}

std::unordered_set<int> result_set;
for (int i = 0; i < nums2.size(); ++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.

ranaged for 文で書いたほうがシンプルに書けると思います。

for (int i : nums2) {
    if (nums1_set.find(i) != nums1_set.end()) {
        result_set.insert(i);
    }
}

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.

@nodchip
コメントありがとうございます。
そうですよね。ranged for文の書き方に慣れておらず避けてしまいました。
たくさん書いて練習します。
https://en.cppreference.com/w/cpp/language/range-for.html?utm_source


std::unordered_set<int> result_set;
for (int i = 0; i < nums2.size(); ++i) {
if (nums1_set.find(nums2[i]) != nums1_set.end()) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

std::set::contains のほうがシンプルに書けると思います。

if (nums1_set.contains(nums2[i])) {

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.

@nodchip
ありがとうございます。
メンバ関数を知らな過ぎますね、もっと知識増やします。
https://en.cppreference.com/w/cpp/container/unordered_set/contains.html?utm_source

for (std::unordered_set<int>::iterator it = result_set.begin(); it != result_set.end(); ++it) {
result.push_back(*it);
}
return result;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

戻り値が std::vector なので、 std::vector のコンストラクターを呼び出す書き方で、シンプルに書けると思います。

return {result_set.begin(), result_set.end()};

ただ、この書き方はあまり見ないかもしれません。

class Solution {
public:
std::vector<int> intersection(std::vector<int>& nums1, std::vector<int>& nums2) {
std::set<int> nums1_set(nums1.begin(), nums1.end());
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

変数名にコンテナ名を入れても、読み手にとってあまり有益ではないかもしれません。
unique_nums1とかはどうでしょうか?

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.

@5103246
ご指摘ありがとうございます。
これは良くないですね。unique_nums1参考にさせて頂きます!

nums1_set.insert(nums1[i]);
}

std::unordered_set<int> result_set;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

結局,vectorに対するpush_back()も償却計算時間はO(1)なので,ここをハッシュマップにするモチベーションがあるのか気になりました.
もちろん係数部分がどちらが大きいか次第ではありますが,そもそもこの箇所がコード全体のボトルネックになるとは考えにくいとも思います.


class Solution {
public:
std::vector<int> intersection(std::vector<int>& nums1, std::vector<int>& nums2) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nums1, nums2は内部での変更がないため引数は非const参照にしてもよいかと思います。

std::vector<int> intersection(std::vector<int>& nums1, std::vector<int>& nums2) {
std::set<int> nums1_set(nums1.begin(), nums1.end());

std::set<int> intersection_set;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

名前がよく似ている関数でstd::set_intersectionがあります。
https://cpprefjp.github.io/reference/algorithm/set_intersection.html
nums1, nums2をsetに変換すると本問題で使える形になります。

Comment on lines +5 to +8
1. 2つの整数配列nums1, nums2
2. 両方の配列に共通して存在する要素のみを返して
3. 結果の各要素は、重複なしであること
4. 返す順番は任意で良い
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nit: space4個分でインデントなので,もう一つtabを入れるかスペース2つを入れるかしないとインデントにならないですね(自分の環境でもtab1回でインデントできる時とtab2回必要な時があって困惑しています.)

Comment on lines +31 to +34
std::unordered_set<int> nums1_set;
for (int i = 0; i < nums1.size(); ++i) {
nums1_set.insert(nums1[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.

コンストラクタを使うとシンプルに書けます:

std::unordered_set<int> nums1_set(nums.begin(), nums2.end());

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.

5 participants