Skip to content

Create TwySum.md#1

Open
aiueoriku wants to merge 4 commits intomainfrom
TwoSum
Open

Create TwySum.md#1
aiueoriku wants to merge 4 commits intomainfrom
TwoSum

Conversation

@aiueoriku
Copy link
Copy Markdown
Owner

No description provided.

Comment thread TwoSum/TwoSum.md
Comment on lines +38 to +39
print(nums) # [2, 7, 11, 15]
print(hashmap) # {15: 3, 2: 0, 11: 2, 7: 1}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Print()必要ないです

Comment thread TwoSum/TwoSum.md
for j in range(i+1, len(nums)):
if nums[i]+nums[j]==target:
return [i,j]
break
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

returnしたあとはbreak読まれないので必要ないです

Comment thread TwoSum/TwoSum.md
Comment on lines +14 to +16
for j in range(i+1, len(nums)):
if nums[i]+nums[j]==target:
return [i,j]
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://peps.python.org/pep-0008/#whitespace-in-expressions-and-statements

Comment thread TwoSum/TwoSum.md
Comment on lines +36 to +44
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}
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 []
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

二つforループ書くのではなく以下のようにすれば一つのforループで簡潔できます

num_to_index = {}
for i in range(len(nums)):
    complement = target - nums[i]
    if complement in num_to_index:
        return [num_to_index[complement], i]
    num_to_index[nums[i], i]

# NOTE 余力があるなら異常な入力を想定してraiseする

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.

公式解答に倣ってforループ2つ書いたのですが,こちらのほうが簡潔ですね.ありがとうございます.

@tokuhirat
Copy link
Copy Markdown

ウェブフックの設定ができていないかもしれません。練習会参加マニュアルをご参照ください。
https://docs.google.com/document/d/1bjbOSs-Ac0G_cjVzJ2Qd8URoU_0BNirZ8utS3CUAeLE/edit?tab=t.0#heading=h.y1cq1o8g4nbl
参考になる記事です。
https://note.com/iroha_afternoon/n/ne741c6d6f84e

コメント集やdiscordで他の人が書いたコードを(大変ではない程度に)読むことも勉強になるのでおすすめです。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.gp4hkfr3qfqc

Comment thread TwoSum/TwoSum.md
class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: 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になっている気がします。

Comment thread TwoSum/TwoSum.md

# Step 3
何も見ないで実装
```python
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 になっていますか、Python3 になっていますか。3にしたほうがいいでしょう。

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.

ご指摘ありがとうございます.python2になっているようでしたので,次回からpython3にします.

Comment thread TwoSum/TwoSum.md
return [i, hashmap[complement]]
return []
```
実行時間は5ms同じコードなのに実行時間が違う.他に見るべき指標があるかも.
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.isflp7vsmzk2

一方で、実行時間がどれくらい問題になるのかは考えるべきです。

Comment thread TwoSum/TwoSum.md


# Step 2
PRや公式解答を参照.
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

LeetCode 公式は、問題があるコードが多いので、分かっていないならば参考にしないほうがいいです。

また、何かを参考にしたらそれについてのリンクを張っておきましょう。

Comment thread TwoSum/TwoSum.md
: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.

意味に基づいて変数名をつけてあげるとわかりやすくなると思います。例えば num_to_index などがありうると思います。

Comment thread TwoSum/TwoSum.md
:rtype: List[int]
"""
hashmap = {}
for i in range(len(nums)):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

好みかもしれませんが、enumerate を使うこともできますね。
https://docs.python.org/3.13/library/functions.html#enumerate

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.

試してみます.ありがとうございます.

Comment thread TwoSum/TwoSum.md
return [i, hashmap[complement]]
return []
```
実行時間は5ms同じコードなのに実行時間が違う.他に見るべき指標があるかも.
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

LeetCode 上の実行時間はブレが大きいらしいので目安程度と思った方が良さそうです。

いただいたアドバイスを元にStep4を追記
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