Skip to content

1. Two Sum#11

Open
tom4649 wants to merge 1 commit intomainfrom
1.Two-Sum
Open

1. Two Sum#11
tom4649 wants to merge 1 commit intomainfrom
1.Two-Sum

Conversation

@tom4649
Copy link
Copy Markdown
Owner

@tom4649 tom4649 commented Mar 14, 2026

:type target: int
:rtype: List[int]
"""
hashmap = {}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

何をキーにして何を値にするかが分かりやすいような変数名をつけるのをお勧めします。
お勧めする理由は、そうすることでその変数で何をするのかが一目で分かり、読み手が処理が追いやすくなるからです。処理が追いやすいと、
今後この箇所を変更する必要があるときにどんな変更をすれば良いかが分かりやすくなったり、変更をしたときに想定外の挙動変化が起きないことの確認がしやすくなったりします。

よく見る命名の仕方はキーがXXXで値がYYYなら"XXX_to_YYY"とかでしょうか。
今回の例なら値が「過去見てきたインデックス値i」で、キーが「nums[i]と足し合わせてtargetと等しくなる数(補数、complement)」ですから、例えば
"complement_to_indices"でしょうか。
一方で、この命名だと誰にとってのcomplementなのか、complementとindexの関係はなんなのか、の2点が分かりにくいと感じる人もいるかもしれませんね。

命名を頑張る代わりに処理を少し変えると幾分かは分かりやすい命名ができるかもしれません;
補数を値にする代わりに、nums[i]そのものを値にすれば、numsが記録しているもの=numをキーに、値をそのインデックスに、ということで"num_to_indices"とかになりますかね。キーと値の関係が分かりやすい分、これくらいの命名でも十分分かりやすいと思います

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.

num_to_indicesは良さそうですね。変数名を気にしない癖がついてしまったので、改善できるように練習します

Copy link
Copy Markdown

@arahi10 arahi10 left a comment

Choose a reason for hiding this comment

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

コメントをつけました。よろしくお願いします。

# 1. Two Sum

- [1. Two Sum](https://leetcode.com/problems/two-sum/description/)
- 愚直にsol1.pyを書いてしまった
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

念のため確認させてください。sol1.py を書く前に時間計算量を求めましたか?また、時間計算量から処理時間を推定しましたか?コードを書く前に時間計算量を求め、処理時間を推定することをお勧めいたします。
処理時間の推定方法については、以下のコメントをご参照ください。
Yuto729/LeetCode_arai60#16 (comment)

Comment on lines +4 to +6
:type nums: List[int]
:type target: int
:rtype: 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.

また、Python2 になっていませんか?

for i, num in enumerate(nums):
if num in hashmap:
return [i, hashmap[num]]
hashmap[target - num] = 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.

私が後いいたいのは見つからなかったときにどうするかですね。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.z4zz4wpn0zz0

異常な入力が来た時にどうするか。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.jdtk9v35bca4

数字の場合は、だんだんいろいろな事情で大きな数字が入るようになってきて、気がついたら、ここにその数が流れてきて、10001 を超えていたという事が起きるでしょう。で、事故を起こして、顧客に代表が謝罪をしているときに、2年前にこのコードを書いたときには、10000までしか来ないって言われていたので、このコードは悪くありません、となるかということですね。

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