Skip to content

1 two sum#12

Open
MA-yo-TA wants to merge 5 commits into
mainfrom
1-Two-Sum
Open

1 two sum#12
MA-yo-TA wants to merge 5 commits into
mainfrom
1-Two-Sum

Conversation

@MA-yo-TA

Copy link
Copy Markdown
Owner

Comment thread 1-Two-Sum/note.md
Comment on lines +40 to +43
- ソートする方とはなんだろう?
- ソートして、先頭と末尾の和から見ていって↓を繰り返すとか?
- 和が target より大きいなら、後ろのポインタを一つ前に
- 〃小さいなら、前のポインタを一つ後ろに

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

他の人のコードも見つけてきましたが,言及されている通りのようですね
https://github.com/fuga-98/arai60/pull/12/changes#diff-e05ee5891f6b31ddafd0fa4d5497330fedc2954d82a170f4b6fc95f8f48d4d2cR41-R64

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.

ありがとうございます、そのようですね。
この方のコードで、辞書に詰めなくて「set で存在確認→ list.index() で index を取得」というのもなるほどと思いました。

Comment thread 1-Two-Sum/step3.py
Comment on lines +1 to +9
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
num_to_index = {}
for index, num in enumerate(nums):
pair_index = num_to_index.get(target - num)
if pair_index is None:
num_to_index[num] = index
continue
return [index, pair_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.

自分のコードは2回ループしてたので,違う書き方で参考になりました.
miyataka/coding-practice#11

同一indexでないというチェックがないのはなぜだろう,というようなことを考えてたりしましたが,1回ループverだと不要になるんですね.

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.

私も最初はそうしてたのですが、 huyfififi/coding-challenges#1 を見てなるほどと思い、真似してみました

Comment thread 1-Two-Sum/step3.py
def twoSum(self, nums: List[int], target: int) -> List[int]:
num_to_index = {}
for index, num in enumerate(nums):
pair_index = num_to_index.get(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という書き方をされている方も見かけました。

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/step1.py

class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
num_to_indices: Dict[int, List[int]] = {}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Python 3.9 以降では dict[int, list[int]] と書けるようです。

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.

ありがとうございます。そのようですね。↓のコメントでも言及されていますね。

huyfififi/coding-challenges#1 (comment)

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