-
Notifications
You must be signed in to change notification settings - Fork 0
349. Intersection of Two Arrays #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TakayaShirai
wants to merge
1
commit into
main
Choose a base branch
from
349_intersection_of_two_arrays
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| // Step 1 | ||
| // 手作業でどうやるか考える | ||
| // まず、intersection について把握。 | ||
| // intersection は 二つの数の集合の中で、重複している数字の集合 | ||
| // この intersection を、数の集合を二つ与えられたときに、導ければ良い。 | ||
| // | ||
| // 単純に思いつくのは、片方の集合(集合Aとする)を手元に置きながら、もう片方の集合(集合Bとする)の数字を一つずつ見ていく。 | ||
| // そのとき、集合Bの数字が集合Aに入っているかを確認して、集合Aにあれば、intersection の要素と判断する。(すでに intersection として追加されていた場合は、それは追加しない) | ||
| // これを、集合B の数字全てに対して行えば、intersection が得られる。 | ||
| // | ||
| // もしくは、二つの集合をまず昇順でソートする。 | ||
| // その後、二つの集合のうち一番小さい数字を比較する。 | ||
| // 同じであれば、intersection の要素として判断。 | ||
| // 数字が小さい方は、一つ大きい数字に増やす。 | ||
| // これを片方の集合の一番大きい数字に辿り着いて、比較をするまで行う。 | ||
| // | ||
| // それ以外の方法はパッと思いつかなかった。 | ||
| // 最初の方法としては、最初に Set を作ってしまえば、比較自体はO(1)ですんで、それをもう片方の集合の数の分(O(n))だけ比較をすれば良い。 | ||
| // 手法がこちらの方が単純に感じるため、こちらでとく。 | ||
|
|
||
| class Solution { | ||
| List<int> intersection(List<int> nums1, List<int> nums2) { | ||
| var uniqueNums = nums1.toSet(); | ||
| var intersection = <int>{}; | ||
|
|
||
| for (var num in nums2) { | ||
| if (uniqueNums.contains(num)) { | ||
| intersection.add(num); | ||
| } | ||
| } | ||
|
|
||
| return intersection.toList(); | ||
| } | ||
| } | ||
|
|
||
| // Step2: | ||
| // 他の人のコードを読む。 | ||
| // | ||
| // https://github.com/Yuto729/LeetCode_arai60/pull/18/changes#diff-28c119488e952cedcb1564a4346e460ac7401048a03e2384b1f9ce9e4149476bR7 | ||
| // 空集合がある場合に、早期リターンで空の配列を返していた。確かにこれをすべき。 | ||
| // | ||
| // https://github.com/katataku/leetcode/pull/12#discussion_r1893968021 | ||
| // > 片方がとても大きくて、片方がとても小さいときには、大きい方を set にするのは大変じゃないでしょうか、特に大きいほうが sort 済みのときにはどうしますか。 | ||
| // 小さい方を Set で用意して、大きい方でイテレーションをしていく。 | ||
| // 大きい方がソート済みの場合は、小さい方の値をピックアップして、大きい方でバイナリサーチをしていけば、大きい方を Set にするという大変な作業が減る。 | ||
| // | ||
| // 早期リターン + 小さい方の Set の用意 | ||
| class Solution { | ||
| List<int> intersection(List<int> nums1, List<int> nums2) { | ||
| if (nums1.isEmpty || nums2.isEmpty) { | ||
| return []; | ||
| } | ||
|
|
||
| if (nums1.length > nums2.length) { | ||
| var tmp = nums1; | ||
| nums1 = nums2; | ||
| nums2 = tmp; | ||
| } | ||
|
|
||
| var uniqueNums = nums1.toSet(); | ||
| var intersection = <int>{}; | ||
|
|
||
| for (var num in nums2) { | ||
| if (uniqueNums.contains(num)) { | ||
| intersection.add(num); | ||
| } | ||
| } | ||
|
|
||
| return intersection.toList(); | ||
| } | ||
| } | ||
|
|
||
| // バイナリサーチで探索していく手法 (ソートされていることを想定するために、nums2 はソートしておく。実際はしない。) | ||
| class Solution { | ||
| List<int> intersection(List<int> nums1, List<int> nums2) { | ||
| if (nums1.isEmpty || nums2.isEmpty) { | ||
| return []; | ||
| } | ||
|
|
||
| if (nums1.length > nums2.length) { | ||
| var tmp = nums1; | ||
| nums1 = nums2; | ||
| nums2 = tmp; | ||
| } | ||
|
|
||
| nums2.sort(); // 実際は、この処理はいらない。 | ||
| var intersection = <int>{}; | ||
|
|
||
| for (var num in nums1) { | ||
| if (existNum(num, nums2)) { | ||
| intersection.add(num); | ||
| } | ||
| } | ||
|
|
||
| return intersection.toList(); | ||
| } | ||
|
|
||
| bool existNum(int target, List<int> nums) { | ||
| int low = 0; | ||
| int high = nums.length - 1; | ||
|
|
||
| while (low <= high) { | ||
| int mid = low + (high - low) ~/ 2; | ||
|
|
||
| if (nums[mid] == target) { | ||
| return true; | ||
| } else if (nums[mid] < target) { | ||
| low = mid + 1; | ||
| } else { | ||
| high = mid - 1; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
|
|
||
| // Step 3: | ||
| class Solution { | ||
| List<int> intersection(List<int> nums1, List<int> nums2) { | ||
| if (nums1.isEmpty || nums2.isEmpty) { | ||
| return []; | ||
| } | ||
|
|
||
| if (nums1.length > nums2.length) { | ||
| var tmp = nums1; | ||
| nums1 = nums2; | ||
| nums2 = tmp; | ||
| } | ||
|
|
||
| var uniqueNums = nums1.toSet(); | ||
| var intersection = <int>{}; | ||
|
|
||
| for (var num in nums2) { | ||
| if (uniqueNums.contains(num)) { | ||
| intersection.add(num); | ||
| } | ||
| } | ||
|
|
||
| return intersection.toList(); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nits]
重箱の隅をつつくようなコメントで恐縮ですが(他は特にコメントしようもないほど充実していました)…
nums1にする」なら、その意図まで命名に反映してはどうかということで、
とかはどうでしょうか。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
レビューありがとうございます!
確かに非対称的な変数に対して、
nums1,nums2などの対称を想起しそうな命名はあまり良くなかったです。括り出す方針も賛成です!