-
Notifications
You must be signed in to change notification settings - Fork 0
Solve 1. Two Sum (step 1, 2) #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cheeseNA
wants to merge
2
commits into
main
Choose a base branch
from
1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 と合わないので[]を採用することにした. 実務上本来どうするべきなのかはまだ分からない. | ||
|
||
ほかは appeared_number_to_index の命名だが, \_table としている人もいた. table であることは{}で初期化しているためなくても良いかと思い, そのままにした. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 [] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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番目の返り値でエラーかどうかを返してしまうという方法まで色々バリエーションがあります。一般的に例外だと大域脱出ができるので処理が簡単に書けますが、ソースコードを追いづらくなります。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
早速レビューありがとうございます!
なるほど, いくつか選択肢がありその中からチームに合ったものを選ぶという感じなのですね. 確かにコードベースの中で作法が統一されていれば, 返り値がNoneだろうが[]だろうが親側のifがちょっと変わるぐらいの差ですもんね.