Conversation
| } | ||
|
|
||
| std::unordered_set<int> result_set; | ||
| for (int i = 0; i < nums2.size(); ++i) { |
There was a problem hiding this comment.
ranaged for 文で書いたほうがシンプルに書けると思います。
for (int i : nums2) {
if (nums1_set.find(i) != nums1_set.end()) {
result_set.insert(i);
}
}There was a problem hiding this comment.
@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()) { |
There was a problem hiding this comment.
std::set::contains のほうがシンプルに書けると思います。
if (nums1_set.contains(nums2[i])) {There was a problem hiding this comment.
@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; |
There was a problem hiding this comment.
戻り値が 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()); |
There was a problem hiding this comment.
変数名にコンテナ名を入れても、読み手にとってあまり有益ではないかもしれません。
unique_nums1とかはどうでしょうか?
There was a problem hiding this comment.
@5103246
ご指摘ありがとうございます。
これは良くないですね。unique_nums1参考にさせて頂きます!
| nums1_set.insert(nums1[i]); | ||
| } | ||
|
|
||
| std::unordered_set<int> result_set; |
There was a problem hiding this comment.
結局,vectorに対するpush_back()も償却計算時間はO(1)なので,ここをハッシュマップにするモチベーションがあるのか気になりました.
もちろん係数部分がどちらが大きいか次第ではありますが,そもそもこの箇所がコード全体のボトルネックになるとは考えにくいとも思います.
|
|
||
| class Solution { | ||
| public: | ||
| std::vector<int> intersection(std::vector<int>& nums1, std::vector<int>& nums2) { |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
名前がよく似ている関数でstd::set_intersectionがあります。
https://cpprefjp.github.io/reference/algorithm/set_intersection.html
nums1, nums2をsetに変換すると本問題で使える形になります。
| 1. 2つの整数配列nums1, nums2 | ||
| 2. 両方の配列に共通して存在する要素のみを返して | ||
| 3. 結果の各要素は、重複なしであること | ||
| 4. 返す順番は任意で良い |
There was a problem hiding this comment.
nit: space4個分でインデントなので,もう一つtabを入れるかスペース2つを入れるかしないとインデントにならないですね(自分の環境でもtab1回でインデントできる時とtab2回必要な時があって困惑しています.)
| std::unordered_set<int> nums1_set; | ||
| for (int i = 0; i < nums1.size(); ++i) { | ||
| nums1_set.insert(nums1[i]); | ||
| } |
There was a problem hiding this comment.
コンストラクタを使うとシンプルに書けます:
std::unordered_set<int> nums1_set(nums.begin(), nums2.end());
This: https://leetcode.com/problems/intersection-of-two-arrays/
Next: https://leetcode.com/problems/unique-email-addresses/description/