Open
Conversation
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order.
Refactor twoSum function to use a hash map for O(n) time complexity. Update error handling and variable naming for clarity.
Added analysis of time and space complexity for the Two Sum problem. Discussed the inefficiency of O(n^2) solutions and considerations for using itertools.
Updated explanations and code comments for the Two Sum problem, clarifying time and space complexity analysis.
oda
reviewed
Feb 24, 2026
| 時間計算量これならO(n)だし、num_to_indexに追加していくので最悪空間計算量O(n) | ||
| Hashのカテゴリに入っていたし辞書使いそうなのは思いついたが、target - numで一致するものを探すというのは思いつかなかった。 | ||
| コメントで指摘されていたようにcomplementつけたほうがわかりやすそう。 | ||
| ValueErrorが標準なのでraiseではそっちを使ったほうが良さそう。 |
There was a problem hiding this comment.
https://docs.python.org/3/library/exceptions.html
エラーはこれですね。
itertools を docs で調べているの、とてもよい欲求と思います。
| for i in range(len(nums)): | ||
| for j in range(i + 1 , len(nums)): | ||
| if nums[i] + nums[j] == target: | ||
| result_list.extend([i, j]) |
There was a problem hiding this comment.
extendよりappendの方が自然だと思いました. (あるいはextendを使いたい場合は, result_listではなく, index_pairのように変数名を変更した方が適切だと感じました)
extendはappendを複数回するような動作です
https://docs.python.org/3/tutorial/datastructures.html#:~:text=list.extend(iterable)
Extend the list by appending all the items from the iterable
result_listはresultとなる候補を入れるリストだとよめて, この書き方はiという結果とjという結果をそれぞれappendしているような感覚です.
上記は全て解が複数個想定される問題設定だったときの話です
Owner
Author
There was a problem hiding this comment.
ありがとうございます!
おっしゃる通りでリストを作って、バラしてリストに入れるという2度手間になるのでextendである必要ないですねmm
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
問題文
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
You can return the answer in any order.
url: https://leetcode.com/problems/two-sum/description/