Skip to content
Open
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions 1/memo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# 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)

- ハッシュを使うのが模範解答
- https://discord.com/channels/1084280443945353267/1201211204547383386/1207251531041210408

11 changes: 11 additions & 0 deletions 1/sol1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: 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]
12 changes: 12 additions & 0 deletions 1/sol2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
Comment on lines +4 to +6
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 になっていませんか?

"""
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は良さそうですね。変数名を気にしない癖がついてしまったので、改善できるように練習します

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までしか来ないって言われていたので、このコードは悪くありません、となるかということですね。