-
Notifications
You must be signed in to change notification settings - Fork 0
15. 3Sum #30
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
ryosuketc
wants to merge
1
commit into
main
Choose a base branch
from
15_3sum
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
15. 3Sum #30
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,28 @@ | ||
| # 15. 3Sum | ||
|
|
||
| https://leetcode.com/problems/3sum/ | ||
|
|
||
| ## Comments | ||
|
|
||
| ### step1 | ||
|
|
||
| * 1 つ固定して two sum するか、sort する方法もありそうだな (一瞬 binary search が頭をよぎった)、などと考える | ||
| * `Solution` を書いてみたが、 重複を許さないの考慮していなかった (認識はしていたが、重複排除についてしっかり考えられていなかった) | ||
| * two sum 部分を関数に切り出すkと主考えたが、`triplets` を引数で受け渡して書き込めるようにするのか、2 要素の `vector<vector<int>>` を返して `nums[i]` につけるのか、値の返し方で迷ったので一旦関数化はせずに書くことにした。 | ||
| * 後で考えると、このコードには重複以外にも問題があって、set を使っているので、出現回数を考慮 (異なるインデックスの値をすべて網羅) していない。 | ||
| * e.g. `nums = [0, 0, 0, 0]` のとき、`i = 0` で固定すると、本来は `{0,1,2}, {0,1,3}, {0,2,3}` (index) の 3 つが見つかってほしいけど、`{0, 1, 2}, {0, 1, 3}` しか追加されない | ||
| * 重複排除なら sort するか、と思ったが 12 分くらい経っていたので step2 へ。 | ||
|
|
||
| ### step2 | ||
|
|
||
| * 一応 sort してやれば `step1.Solution` のアプローチのままでも重複排除は可能 | ||
| * `Solution1` | ||
| * ただ、どうせ sort するなら two pointer 的なアプローチのほうが良さそう -> `Solution2` | ||
| * TODO: 一応 sort せずに解く方法もありそうだが、時間がなさそうなので今回はスキップ。 | ||
| * 探査して結果を set に入れるようなことをすればよさそうではある | ||
| * https://leetcode.com/problems/3sum/submissions/1882175465/ | ||
|
|
||
|
|
||
| ### step3 | ||
|
|
||
| * skip |
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,18 @@ | ||
| class Solution { | ||
| public: | ||
| vector<vector<int>> threeSum(vector<int>& nums) { | ||
| vector<vector<int>> triplets; | ||
| for (int i = 0; i < nums.size(); ++i) { | ||
| int target_num = -nums[i]; // nums[i] - 0 | ||
| std::unordered_set<int> seen; | ||
| for (int j = i + 1; j < nums.size(); ++j) { | ||
| int complement = target_num - nums[j]; | ||
| if (seen.contains(complement)) { | ||
| triplets.push_back({nums[i], nums[j], complement}); | ||
| } | ||
| seen.insert(nums[j]); | ||
| } | ||
| } | ||
| return triplets; | ||
| } | ||
| }; |
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,64 @@ | ||
| // sort + hashset | ||
| #include <algorithm> | ||
|
|
||
| class Solution { | ||
| public: | ||
| vector<vector<int>> threeSum(vector<int>& nums) { | ||
| std::sort(nums.begin(), nums.end()); | ||
| vector<vector<int>> triplets; | ||
| for (int i = 0; i < nums.size(); ++i) { | ||
| if (i > 0 && nums[i] == nums[i - 1]) continue; | ||
|
|
||
| int target_num =-nums[i]; // nums[i] - 0 | ||
| std::unordered_set<int> seen; | ||
| for (int j = i + 1; j < nums.size(); ++j) { | ||
| int complement = target_num - nums[j]; | ||
| if (seen.contains(complement)) { | ||
| triplets.push_back({nums[i], nums[j], complement}); | ||
| while (j + 1 < nums.size() && nums[j] == nums[j + 1]) { | ||
| ++j; | ||
| } | ||
| } | ||
| seen.insert(nums[j]); | ||
| } | ||
| } | ||
| return triplets; | ||
| } | ||
| }; | ||
|
|
||
| // sort + two pointers | ||
| #include <algorithm> | ||
|
|
||
| class Solution2 { | ||
| public: | ||
| vector<vector<int>> threeSum(vector<int>& nums) { | ||
| vector<vector<int>> result; | ||
| std::sort(nums.begin(), nums.end()); | ||
| for (int i = 0; i < nums.size(); ++i) { | ||
| // To avoid duplicates (skip the same number) | ||
| if (i > 0 && nums[i - 1] == nums[i]) continue; | ||
|
|
||
| int left = i + 1; | ||
| int right = nums.size() - 1; | ||
| while (left < right) { | ||
| int sum = nums[i] + nums[left] + nums[right]; | ||
| if (sum < 0) { | ||
| ++left; | ||
| continue; | ||
| } | ||
| if (sum > 0) { | ||
| --right; | ||
| continue; | ||
| } | ||
| // sum == 0 | ||
| result.push_back({nums[i], nums[left], nums[right]}); | ||
|
|
||
| while (left < right && nums[left] == nums[left + 1]) ++left; | ||
| while (left < right && nums[right - 1] == nums[right]) --right; | ||
| ++left; | ||
| --right; | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
| }; | ||
Empty file.
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.
right は変更しなくても、
でカバーできそうです。ただ、ロジックの分かりやすさを考慮し、明示的に変更しても良いと思います。