-
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
hemispherium
wants to merge
1
commit into
main
Choose a base branch
from
0349-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,13 @@ | ||
| ### step1 | ||
|
|
||
| nums1とnums2をsetに入れて、制約である0-1000までの値の中で両方のsetに | ||
| 時間計算量はnums1, nums2の長さをN,MとしてO(NlogN + MlogM + 1000 * (logN + logM))になると思います。 | ||
|
|
||
| ### step2 | ||
|
|
||
| step1のコードをGPTにより簡潔で高速なコードに修正してもらいました。 | ||
| 時間計算量はO(N + M)に改善されると思います。 | ||
|
|
||
| ### step3 | ||
|
|
||
| step2のコードを3回通すまで書き直し。 |
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,19 @@ | ||
| class Solution { | ||
| public: | ||
| vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { | ||
| set<int> set1, set2; | ||
| for (int num1 : nums1) { | ||
| set1.insert(num1); | ||
| } | ||
| for (int num2 : nums2) { | ||
| set2.insert(num2); | ||
| } | ||
| vector<int> answer; | ||
| for (int i = 0; i <= 1000; i++) { | ||
| if (set1.contains(i) && set2.contains(i)) { | ||
| answer.push_back(i); | ||
| } | ||
| } | ||
| return answer; | ||
| } | ||
| }; | ||
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,15 @@ | ||
| class Solution { | ||
| public: | ||
| vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { | ||
| unordered_set<int> set_of_nums1(nums1.begin(), nums1.end()); | ||
| unordered_set<int> answer; | ||
|
|
||
| for (int num2 : nums2) { | ||
| if (set_of_nums1.count(num2)) { | ||
| answer.insert(num2); | ||
| } | ||
| } | ||
|
|
||
| return vector<int>(answer.begin(), answer.end()); | ||
| } | ||
| }; |
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,15 @@ | ||
| class Solution { | ||
| public: | ||
| vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { | ||
| unordered_set<int> set_of_nums1(nums1.begin(), nums1.end()); | ||
| unordered_set<int> answer; | ||
|
|
||
| for (int num2 : nums2) { | ||
| if (set_of_nums1.count(num2)) { | ||
| answer.insert(num2); | ||
| } | ||
| } | ||
|
|
||
| return vector<int>(answer.begin(), answer.end()); | ||
| } | ||
| }; |
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.
これでも制約が保証されているならばいいですが、だいたいこういう制約は気がつくと破られているものなので、破られても耐えられるものにしておくといいでしょう。
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.
承知しました!