-
Notifications
You must be signed in to change notification settings - Fork 0
1. Two Sum #11
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?
1. Two Sum #11
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,7 @@ | ||
| # 1. Two Sum | ||
|
|
||
| - [1. Two Sum](https://leetcode.com/problems/two-sum/description/) | ||
| - 愚直にsol1.pyを書いてしまった | ||
| - ハッシュを使うのが模範解答 | ||
| - https://discord.com/channels/1084280443945353267/1201211204547383386/1207251531041210408 | ||
|
|
||
| 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] |
| 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
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 になっていませんか? |
||
| """ | ||
| 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. 何をキーにして何を値にするかが分かりやすいような変数名をつけるのをお勧めします。 よく見る命名の仕方はキーがXXXで値がYYYなら"XXX_to_YYY"とかでしょうか。 命名を頑張る代わりに処理を少し変えると幾分かは分かりやすい命名ができるかもしれません;
Owner
Author
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_indicesは良さそうですね。変数名を気にしない癖がついてしまったので、改善できるように練習します |
||
| for i, num in enumerate(nums): | ||
| if num in hashmap: | ||
| return [i, hashmap[num]] | ||
| hashmap[target - num] = i | ||
|
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 comment
The reason will be displayed to describe this comment to others. Learn more.
念のため確認させてください。sol1.py を書く前に時間計算量を求めましたか?また、時間計算量から処理時間を推定しましたか?コードを書く前に時間計算量を求め、処理時間を推定することをお勧めいたします。
処理時間の推定方法については、以下のコメントをご参照ください。
Yuto729/LeetCode_arai60#16 (comment)