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
27 changes: 27 additions & 0 deletions 1. Two Sum/note.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
解いた問題だったので解法は知っていた.

# step1

time: 09:30

とりあえず O(N^2)案は思いつく.

表れた数字を set に持っておけば, 数字を見たときに target-num がすでに表れてるか O(1)でチェックできる.

time complexity は O(N)

space complexity は O(N)

only one valid answer exists とのことなので error 処理は考えなくて良い. しかし return None はいかがなものか

命名が不安だ, set まで変数名に含めるべきか.

indices というのを見ていなくて 1 エラー. それに伴い set も hashmap に変更.

# step2

他の人の解法などを見て改善点を考える.

return None は type annotation と合わないので[]を採用することにした. 実務上本来どうするべきなのかはまだ分からない.

Choose a reason for hiding this comment

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

唯一の正解がある訳でなくて、実際はチームの方針などにもよるかなと思います。Noneが返ってくる場合がエラー、空配列が返ってくる場合がエラー、[-1, -1]のように返ってくる場合はエラーなどありますがある程度は決めの問題かなと思います。静的型付け言語などでは値でエラーを表すことが多い気がします。PythonだとNoneをエラーとしてしまっても良いように思います。

あとは他にも例外を投げちゃう方法やPythonだと関数の返り値が複数取れるのでいっそGo言語みたいに2番目の返り値でエラーかどうかを返してしまうという方法まで色々バリエーションがあります。一般的に例外だと大域脱出ができるので処理が簡単に書けますが、ソースコードを追いづらくなります。

Copy link
Owner Author

Choose a reason for hiding this comment

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

早速レビューありがとうございます!
なるほど, いくつか選択肢がありその中からチームに合ったものを選ぶという感じなのですね. 確かにコードベースの中で作法が統一されていれば, 返り値がNoneだろうが[]だろうが親側のifがちょっと変わるぐらいの差ですもんね.


ほかは appeared_number_to_index の命名だが, \_table としている人もいた. table であることは{}で初期化しているためなくても良いかと思い, そのままにした.
18 changes: 18 additions & 0 deletions 1. Two Sum/old.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
length = len(nums)
num_indexes = [(nums[i], i) for i in range(length)]
sorted_num_indexes = sorted(num_indexes)
small_idx = 0
large_idx = length - 1
while small_idx < large_idx:
temporary_sum = sorted_num_indexes[small_idx][0] + sorted_num_indexes[large_idx][0]
if temporary_sum == target:
return [sorted_num_indexes[small_idx][1], sorted_num_indexes[large_idx][1]]
elif temporary_sum < target:
small_idx += 1
else:
large_idx -= 1
raise Error


8 changes: 8 additions & 0 deletions 1. Two Sum/step1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
appeared_number_to_index = {}
for index, num in enumerate(nums):
if target - num in appeared_number_to_index:
return [index, appeared_number_to_index[target - num]]
appeared_number_to_index[num] = index
return None
8 changes: 8 additions & 0 deletions 1. Two Sum/step2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
appeared_number_to_index = {}
for index, num in enumerate(nums):
if target - num in appeared_number_to_index:
return [index, appeared_number_to_index[target - num]]
appeared_number_to_index[num] = index
return []
9 changes: 9 additions & 0 deletions 1. Two Sum/step3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
appeared_num_to_index = {}
for index, num in enumerate(nums):
complement_num = target - num
if complement_num in appeared_num_to_index:
return [index, appeared_num_to_index[complement_num]]
appeared_num_to_index[num] = index
return []