Skip to content

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
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions 1. Two Sum.md
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

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

入力を sort をして頭と尻尾から探していくという手もあるように思います。

- 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`の方が、より適切な名前かもしれない。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

かたおかさんの実装だとそんな気がします。個人的にはnum_to_indexにはnumとそのindexを入れて、要素を取得する際にtarget - numでアクセスするほうが自然なようには思います。