Skip to content

1. Two Sum#11

Open
hiro111208 wants to merge 1 commit into
mainfrom
3-two-sum
Open

1. Two Sum#11
hiro111208 wants to merge 1 commit into
mainfrom
3-two-sum

Conversation

@hiro111208
Copy link
Copy Markdown
Owner

@hiro111208 hiro111208 commented Apr 20, 2026

This problem: 1. Two Sum

Next problem: 349. Intersection of Two Arrays

@hiro111208 hiro111208 changed the title 3. Two Sum 1. Two Sum Apr 20, 2026
Comment thread step1.md
num_to_index = dict()
for i, num in enumerate(nums):
difference = target - num
if difference in num_to_index.keys():
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ここは、

if difference is num_to_index:

でも同じ挙動になるので、そちらのほうが良いかもしれません。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

恐らく正しくは、
if difference in num_to_index:
ですね

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

inが正しいです。
申し訳ありません。

Comment thread step1.md
for i, num in enumerate(nums):
difference = target - num
if difference in num_to_index.keys():
return [i, num_to_index[difference]]
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[num_to_index[difference], i]として、昇順で返した方が自然だと個人的に思います。

Comment thread step3.md
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
num_to_index = dict()
for i, num in enumerate(nums):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

iidxindexの方が、今回行いたいことの意図がより読み手に明示的に伝わるかと思います。

Comment thread step1.md
def twoSum(self, nums: List[int], target: int) -> List[int]:
num_to_index = dict()
for i, num in enumerate(nums):
difference = target - num
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

complement という変数名を使っている方も見かけました。趣味の範囲だと思います。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants