-
Notifications
You must be signed in to change notification settings - Fork 0
Create 349. Intersection of Two Arrays.md #6
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
mt2324
wants to merge
1
commit into
main
Choose a base branch
from
mt2324-patch-3
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
101 changes: 101 additions & 0 deletions
101
349. Intersection of Two Arrays/349. Intersection of Two Arrays.md
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,101 @@ | ||
| https://leetcode.com/problems/intersection-of-two-arrays/ | ||
| ## STEP 1 | ||
| 素直に両方setに変換してintersectionを取る。メモリはO(n)追加でいるけど速い。 | ||
| ```python | ||
| class Solution: | ||
| def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: | ||
| num1_set = set(nums1) | ||
| num2_set = set(nums2) | ||
| ans = [] | ||
| for num in num1_set.intersection(num2_set): | ||
| ans.append(num) | ||
| return ans | ||
| ``` | ||
|
|
||
| ソートしてtwo pointersだけど複数回同じ数字が被った時のために結局setを作んないといけない。あと地味にソートも重い。intersectionが少なくてメモリをできるだけ節約したい時にはいいのか? | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: | ||
| nums1.sort() | ||
| nums2.sort() | ||
| i = 0 | ||
| j = 0 | ||
| intersection = set() | ||
| while i < len(nums1) and j < len(nums2): | ||
| if nums1[i] < nums2[j]: | ||
| i += 1 | ||
| elif nums2[j] < nums1[i]: | ||
| j += 1 | ||
| else: | ||
| intersection.add(nums1[i]) | ||
| i += 1 | ||
| j += 1 | ||
| return list(intersection) | ||
| ``` | ||
|
|
||
| Counter で各要素を数えることもできるけどこれは本質的にsetに変換してるのと代わりない。 | ||
| ```python | ||
| from collections import Counter | ||
| class Solution: | ||
| def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: | ||
| counter1 = Counter(nums1) | ||
| counter2 = Counter(nums2) | ||
| intersection = [] | ||
| for num1 in counter1.keys(): | ||
| if num1 in counter2: | ||
| intersection.append(num1) | ||
| return intersection | ||
| ``` | ||
|
|
||
| ## STEP2 | ||
| intersectionは`&`でも`.intersection()`でも取れるが`&`だとset同士でしか取れないが、`.intersection()` だと引数にset以外のiterable objectを取れるらしい。 | ||
| https://docs.python.org/3.14/library/stdtypes.html#set-types-set-frozenset | ||
|
|
||
| あとsetを生成するはhashを計算する必要があって時間がかかるので小さい方をsetにした方がいいのはたしかにと思った。 | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: | ||
| if len(nums1) < len(nums2): | ||
| return list(set(nums1).intersection(nums2)) | ||
| else: | ||
| return list(set(nums2).intersection(nums1)) | ||
| ``` | ||
| ついでにCounterを使った方も書きなおしたり、 | ||
| ```python | ||
| from collections import Counter | ||
| class Solution: | ||
|
|
||
| def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: | ||
| intersection = () | ||
| if len(nums1) < len(nums2): | ||
| counter = Counter(nums1) | ||
| nums_to_iterate = nums2 | ||
| else: | ||
| counter = Counter(nums2) | ||
| nums_to_iterate = nums1 | ||
|
|
||
| intersection = [] | ||
| for num in nums_to_iterate: | ||
| if num in counter and counter[num] > 0: | ||
| intersection.append(num) | ||
| counter[num] = 0 | ||
| return intersection | ||
| ``` | ||
| 分岐なしで書くならこうかな。 | ||
| ```python | ||
| class Solution: | ||
| def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: | ||
| return list(set(nums1) & set(nums2)) | ||
| ``` | ||
| ## STEP3 | ||
| 練習するだけ | ||
| ```python | ||
| class Solution: | ||
| def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: | ||
| if len(nums1) < len(nums2): | ||
| return list(set(nums1).intersection(nums2)) | ||
| else: | ||
| return list(set(nums2).intersection(nums1)) | ||
| ``` | ||
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.
まだでしたら CPython の該当箇所を読んでみると面白いと思います。