-
Notifications
You must be signed in to change notification settings - Fork 0
1. Two Sum.md #10
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
katataku
wants to merge
1
commit into
main
Choose a base branch
from
1.-Two-Sum
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
1. Two Sum.md #10
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,85 @@ | ||
URL: https://leetcode.com/problems/two-sum/ | ||
|
||
# Step 1 | ||
|
||
- 実装時間: 2分 | ||
- 時間計算量: O(n^2) | ||
- 空間計算量: O(1) | ||
|
||
```python | ||
class Solution: | ||
def twoSum(self, nums: List[int], target: int) -> List[int]: | ||
for i in range(len(nums)): | ||
for j in range(i+1, len(nums)): | ||
if nums[i] + nums[j] == target: | ||
return [i, j] | ||
``` | ||
|
||
# Step 2 | ||
|
||
- 参考にしたURL | ||
- https://github.com/Yoshiki-Iwasa/Arai60/pull/10 | ||
- https://github.com/aoshi2025s/leetcode-review/pull/1 | ||
- https://github.com/seal-azarashi/leetcode/pull/11 | ||
- https://github.com/hroc135/leetcode/pull/11 | ||
- https://github.com/rihib/leetcode/pull/8 | ||
- https://github.com/ryo-devz/LeetCode/pull/2 | ||
- https://github.com/ryoooooory/LeetCode/pull/18 | ||
- https://github.com/takumihara/leetcode/pull/1 | ||
- https://github.com/tarinaihitori/leetcode/pull/11 | ||
- https://github.com/haniwachann/leetcode/pull/2 | ||
- https://github.com/philip82148/leetcode-arai60/pull/1 | ||
|
||
- c++でcontainsを使って解いているのを見た。 | ||
- https://github.com/aoshi2025s/leetcode-review/pull/1 | ||
|
||
```python | ||
class Solution: | ||
def twoSum(self, nums: List[int], target: int) -> List[int]: | ||
num_to_index = {} | ||
for i, num in enumerate(nums): | ||
if num in num_to_index: | ||
return [i, num_to_index[num]] | ||
num_to_index[target - num] = i | ||
``` | ||
|
||
- 不正入力の場合どうするのかは、考えておこう。 | ||
- 例えば、今回の関数がパイプラインやCI/CDなどエラー時すぐに担当者に知らせたいような場合はExceptionを投げて上げたほうがいいかもしれません | ||
Exceptionを発生させないのであれば、nilを返しつつ error logを吐いておくのもありかなと思いました | ||
- https://github.com/rihib/leetcode/pull/8/files#r1706918313 | ||
- returnを書かなければNoneを返す | ||
- https://docs.python.org/3/tutorial/controlflow.html#defining-functions | ||
|
||
- `+`の前後のスペースを入れるべきか。 | ||
- Use your better judgment for the insertion of spaces around arithmetic operators | ||
- https://google.github.io/styleguide/pyguide.html#s3.6-whitespace | ||
- 優先順位の異なる演算子を使用する場合は、優先順位がもっとも低い演算子の周囲に空白を追加することを検討してください。独自の判断で使用してください。ただし、複数のスペースは使用しないでください。また、バイナリ演算子の両側には常に同じ量の空白を入れてください。 | ||
- https://peps.python.org/pep-0008/#other-recommendations | ||
- 考えた結果今回は以下のようなのがいいと思った。 | ||
- `if nums[i]+nums[j] == target:`では`==`がもっとも優先度が低いので、`+`の周りはスペースをあけない。 | ||
|
||
```python | ||
class Solution: | ||
def twoSum(self, nums: List[int], target: int) -> List[int]: | ||
for i in range(len(nums)): | ||
for j in range(i+1, len(nums)): | ||
if nums[i]+nums[j] == target: | ||
return [i, j] | ||
``` | ||
|
||
# Step 3 | ||
|
||
- 時間計算量: O(n) | ||
- 空間計算量: O(n) | ||
|
||
```python | ||
class Solution: | ||
def twoSum(self, nums: List[int], target: int) -> List[int]: | ||
num_to_index = {} | ||
for i, num in enumerate(nums): | ||
if num in num_to_index: | ||
return [i, num_to_index[num]] | ||
num_to_index[target - num] = i | ||
``` | ||
|
||
最後に気づいたけど、`num_to_index`より`complement_to_index`の方が、より適切な名前かもしれない。 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. かたおかさんの実装だとそんな気がします。個人的には |
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.
入力を sort をして頭と尻尾から探していくという手もあるように思います。