Skip to content

1. Two Sum #11

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
54 changes: 54 additions & 0 deletions lc1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""step1

思考ログ
愚直にやるならforループを回すだけ
少し工夫するなら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]
```

"""step2
参考にした方のdiscord一覧
- https://github.com/shintaro1993/arai60/pull/15/files?short_path=70b0510#diff-70b05104dc88196abb247b2c62f918005ded7b778d46b818dbf957c243ee211f
- https://github.com/takumihara/leetcode/pull/1/files#diff-0e987cffc3503d02426293fe621110aa1c7f49e3940742ac0e27bd758446c3ea
- https://github.com/fhiyo/leetcode/pull/14/files?short_path=050f7e8#diff-050f7e8a6e8dcab5ada34b1061c96b59923387f92e6582826703e46c59d73557

- enumerateを使うやり方を紹介している人がいたが、本質的には変わらないなと思ったため実装は割愛する。
- 引き算をして何かをするというのは発送として持っていたが実装に活かせなかった。
Copy link

Choose a reason for hiding this comment

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

私がいいたいことはこれくらいです。

https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.w704ahvong3m

「あなたは部屋に入りました。数字の一つ書かれた紙を1000枚あります。その中から和が5000になる2枚を見つけてください。」というときに、100万回足し算しますかねえ。
https://discord.com/channels/1084280443945353267/1183683738635346001/1187304667126976532

どんどん先に進みましょう。

- complementという変数名を使っている人が多数いたが、個人的にあまり使わない言葉であまり見たことない単語だと感じた。単なる勉強不足の可能性もあるので様子を見たいと思い別の変数を名付けた。
- dict型の変数に関しても、XX_to_YYという名前にすることが多そうだと感じた、今回だったらvalue_to_index, num_to_indexなどもありだと思った。
- 今回の問題では明示的に、"You may assume that each input would have exactly one solution"とあったので考えなかったが、エラー時の例外についても頭に入れておくのは良いと思った。(https://discord.com/channels/1084280443945353267/1218740927120674977/1223967037588635658)
"""

```python
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
seen = {}
Copy link

Choose a reason for hiding this comment

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

seen という変数名ですと、 key と value にどのような値が含まれているのか分かりにくいと感じました。 num_to_index 等、 (keyが表すもの)_to_(valueが表すもの) といった命名はいかがでしょうか?

for i in range(len(nums)):
needed_value = target - nums[i]
if needed_value in seen:
return [seen[needed_value], i]

seen[nums[i]] = i
```

# step 3
```python
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
seen = {}
for i in range(len(nums)):
needed_value = target - nums[i]

Choose a reason for hiding this comment

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

nums を使っているので、needed_num もありかなと感じました。

if needed_value in seen:
return [seen[needed_value], i]

seen[nums[i]] = i
```