Skip to content

1. two sum - #1

Open
lightbanana wants to merge 7 commits into
mainfrom
1.-Two-Sum
Open

1. two sum#1
lightbanana wants to merge 7 commits into
mainfrom
1.-Two-Sum

Conversation

@lightbanana

Copy link
Copy Markdown
Owner

No description provided.

Comment thread 1. Two Sum/memo.md
for j in range(i + 1, len(nums)):
if nums[i] + nums[j] == target:
answer = [i, j]
break

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ここでbreakをしているのはループを抜ける意図だと思うのですが、この書き方だと内側のfor jだけを抜けて、外側のfor iは継続します。
答えを得た時点で、return [i, j]で即座に返すのが個人的に良いと思います。

Comment thread 1. Two Sum/memo.md
diff = target - nums[i]
if diff in visited:
answer = [visited[diff], i]
break

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ここについても、答えを得たらすぐにreturnして良いと思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます!納得しました。

Comment thread 1. Two Sum/memo.md
if nums[i] + nums[j] == target:
answer = [i, j]
break
return answer

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 nums[i] + nums[j] == target:
                    return [i, j]

と書いてもいいと思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます!納得しました。

Comment thread 1. Two Sum/memo.md
else:
visited[nums[i]] = i
return answer

@h-masder h-masder Apr 4, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

step1とstep2のどちらのコードも
targetと一致する数のペアがが見つからなかったとき、answerが未定義のままreturnされますね。

Comment thread 1. Two Sum/memo.md
```python
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
visited = {}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

visited という変数名は、グラフの探索等で、探索済み挑戦の集合を格納するために使うことが多いように感じます。また、 dict 型の変数名は、 (キー)_to_(値) という書式で、キーと値にどのようなものが含まれているかを表すのをよく見かけます。 num_to_index はいかがでしょうか?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます!納得しました。

Comment thread 1. Two Sum/memo.md
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
visited = {}
for i in range(len(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.

for i, num in enumerate(nums):

と、インデックスと値を同時にとったほうがシンプルになると思います。

Comment thread 1. Two Sum/memo.md
def twoSum(self, nums: List[int], target: int) -> List[int]:
visited = {}
for i in range(len(nums)):
diff = target - nums[i]

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 という変数名を使っている方も見かけました。趣味の範囲だと思います。

Comment thread 1. Two Sum/memo.md
break
else:
visited[nums[i]] = i
return answer

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

answerという変数名はleetcodeに寄りすぎていて、現実で使われるコードではそれほど好まれないようです。
https://discord.com/channels/1084280443945353267/1358954923672207420/1462241459209240751
https://docs.google.com/document/u/1/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/mobilebasic#h.fcs3httrll4l

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます!納得しました。

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.

5 participants