-
Notifications
You must be signed in to change notification settings - Fork 0
Create TwySum.md #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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# Step 1 | ||
まずは愚直に実行 | ||
|
||
```python | ||
|
||
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] | ||
Comment on lines
+14
to
+16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 演算子の間にはスペースを空けることをお勧めします https://peps.python.org/pep-0008/#whitespace-in-expressions-and-statements |
||
break | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. returnしたあとはbreak読まれないので必要ないです |
||
``` | ||
実行時間は2271ms | ||
|
||
|
||
|
||
# Step 2 | ||
PRや公式解答を参照. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LeetCode 公式は、問題があるコードが多いので、分かっていないならば参考にしないほうがいいです。 また、何かを参考にしたらそれについてのリンクを張っておきましょう。 |
||
解答を見るとhashmapという手法を発見.dictを用いることで高速に探索出来るらしい. | ||
keyの操作に慣れていないので,デバッグしつつ実装 | ||
```python | ||
class Solution(object): | ||
def twoSum(self, nums, target): | ||
""" | ||
:type nums: List[int] | ||
:type target: int | ||
:rtype: List[int] | ||
""" | ||
hashmap = {} | ||
for i in range(len(nums)): | ||
hashmap[nums[i]] = i | ||
print(nums) # [2, 7, 11, 15] | ||
print(hashmap) # {15: 3, 2: 0, 11: 2, 7: 1} | ||
Comment on lines
+38
to
+39
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Print()必要ないです |
||
for i in range(len(nums)): | ||
complement = target - nums[i] | ||
if complement in hashmap and hashmap[complement] != i: # dict[key]=value | ||
return [i, hashmap[complement]] | ||
return [] | ||
Comment on lines
+36
to
+44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 二つforループ書くのではなく以下のようにすれば一つのforループで簡潔できます
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 公式解答に倣ってforループ2つ書いたのですが,こちらのほうが簡潔ですね.ありがとうございます. |
||
``` | ||
実行時間は158ms.計算量を減らすことが出来た. | ||
|
||
# Step 3 | ||
何も見ないで実装 | ||
```python | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Python2 になっていますか、Python3 になっていますか。3にしたほうがいいでしょう。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ご指摘ありがとうございます.python2になっているようでしたので,次回からpython3にします. |
||
class Solution(object): | ||
def twoSum(self, nums, target): | ||
""" | ||
:type nums: List[int] | ||
:type target: int | ||
:rtype: List[int] | ||
""" | ||
hashmap = {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 意味に基づいて変数名をつけてあげるとわかりやすくなると思います。例えば num_to_index などがありうると思います。 |
||
for i in range(len(nums)): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 好みかもしれませんが、enumerate を使うこともできますね。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 試してみます.ありがとうございます. |
||
hashmap[nums[i]] = i | ||
for i in range(len(nums)): | ||
complement = target - nums[i] | ||
if complement in hashmap and hashmap[complement]!=i: | ||
return [i, hashmap[complement]] | ||
return [] | ||
``` | ||
実行時間は5ms同じコードなのに実行時間が違う.他に見るべき指標があるかも. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. あまりその実行時間はあてになりません。また統計ノイズが大きいので時間を計測するときにはしたのようにします。 一方で、実行時間がどれくらい問題になるのかは考えるべきです。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LeetCode 上の実行時間はブレが大きいらしいので目安程度と思った方が良さそうです。 |
||
|
||
# Step 4 | ||
いただいたアドバイスを元に実装 | ||
```python | ||
class Solution: | ||
def twoSum(self, nums: List[int], target: int) -> List[int]: | ||
num_to_index = {} | ||
for i in range (len(nums)): | ||
complement = target - nums[i] | ||
if complement in num_to_index and num_to_index[complement]!=i: | ||
return [i, num_to_index[complement]] | ||
num_to_index[nums[i]] = i | ||
return [] | ||
``` | ||
辞書の名前をnum_to_indexに変更して意味を分かりやすくした. | ||
for文を1つにまとめた. | ||
|
||
```python | ||
class Solution: | ||
def twoSum(self, nums: List[int], target: int) -> List[int]: | ||
num_to_index = {} | ||
for i, num in enumerate(nums): | ||
complement = target - num | ||
if complement in num_to_index and num_to_index[complement]!=i: | ||
return [num_to_index[complement], i] | ||
num_to_index[num] = i | ||
return [] | ||
``` | ||
enumerateでも書いてみた.個人的にはこちらのほうが好みかも. |
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.
Python2になっている気がします。