-
Notifications
You must be signed in to change notification settings - Fork 0
1.two sum #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
arahi10
wants to merge
3
commits into
main
Choose a base branch
from
1.Two_Sum
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
1.two sum #1
Changes from all commits
Commits
Show all changes
3 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,97 @@ | ||
| <!-- PR のタイトルには, LeetCode の問題タイトルを含める.--> | ||
| <!-- レビュー依頼定型文 | ||
| [問題とそのリンク]を解きました. | ||
| [Pull Request の URL] | ||
| レビューをお願いします. | ||
| --> | ||
| # Two Sum <!-- omit in toc --> | ||
|
|
||
| ## 1. 問題 | ||
|
|
||
| ### 1.1. リンク | ||
|
|
||
| <https://leetcode.com/problems/two-sum/> | ||
|
|
||
| ### 1.2. 問題概要 (閲覧制限のある問題の場合のみ) | ||
|
|
||
| ## 2. 次に取り組む問題のリンク | ||
|
|
||
| <https://leetcode.com/problems/search-insert-position> | ||
|
|
||
| ## 3. ステップ1 | ||
|
|
||
| ### 3.1. コード | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def twoSum(self, nums: List[int], target: int) -> List[int]: | ||
| num2idx = {} | ||
| for i, num in enumerate(nums): | ||
| required = target - num | ||
| if required in num2idx: | ||
| return [i, num2idx[required]] | ||
| num2idx[num] = i | ||
|
|
||
| ``` | ||
|
|
||
| ### 3.2. 時間・空間計算量 | ||
|
|
||
| 引数に与えられる配列のサイズを$N$, 配列中のユニークな要素の総数を$M (\le N)$とする. | ||
|
|
||
| 時間$O(N)$, 空間$O(M)\; ( = O(N))$. | ||
|
|
||
| ## 4. ステップ2 | ||
|
|
||
| ### 4.1. コード | ||
|
|
||
| 解が見つからなかった場合の例外処理を追加; | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def twoSum(self, nums: List[int], target: int) -> List[int]: | ||
| num2idx = {} | ||
| for i, num in enumerate(nums): | ||
| required = target - num | ||
| if required in num2idx: | ||
| return [i, num2idx[required]] | ||
| num2idx[num] = i | ||
| raise ValueError("no two sum solution") | ||
|
|
||
| ``` | ||
|
|
||
| ### 4.2. 講師陣のコメントとして想定されること | ||
|
|
||
| - 辞書と線形探索の速度比較 | ||
| - 組み込み関数 `enumerate` の詳細(?) | ||
|
|
||
| ### 4.3. 改善するときに考えたこと | ||
|
|
||
| - 問題では答えの存在を仮定してよかったが,見つからなかった場合の処理をどうしよう. | ||
| - step1時点のコードではNoneが返される. | ||
| - 選択肢 | ||
| - `None`を返す(そのまま).(返り値の型ヒントを`Optional[List[int]]`に変える) | ||
| - `[None, None]` とかにする.(返り値の型ヒントを`List[Optional[int]]`に変える) | ||
| - 例外を出す. | ||
| - どんな例外が適切だっけ,Javaなら`NoSuchElementException`, `IllegalArgumentException` あたりだろうけど. | ||
| - <https://docs.python.org/ja/3/library/exceptions.html#exception-hierarchy>見る→`ValueError("No two sum solution")` くらいかな. | ||
| - エラーメッセージって小文字始まりのほうがいい気がしてきた; | ||
|
|
||
| ```python | ||
| >>> [][100] | ||
| Traceback (most recent call last): | ||
| File "<stdin>", line 1, in <module> | ||
| IndexError: list index out of range | ||
| ``` | ||
|
|
||
| - 検討 | ||
| - `None` 系統は見つからなかった場合のこの関数の挙動を知らない人にとっては,呼び出し側で実際にエラーが出る箇所が`twoSum`の呼び出しとは違う・パッと見でなぜそのエラーが引き起こされたか(`None`が返ってきているから)を理解しにくいエラーメッセージが出そう.この点では例外投げるほうはその点では親切かなぁ.エラーメッセージで最低限自然言語で説明できるし. | ||
| - 実際の業務で書くなら`None`にせよ例外にせよdocstringに仕様を書く前提. | ||
|
|
||
| ### 4.4. 他の人のコードを読んで考えたこと | ||
|
|
||
| - uehara さん <https://github.com/X-XsleepZzz/leetcode/pull/12/changes>, Mari さん <https://github.com/mt2324/leetcode/pull/2/changes/4cbafe39511752cc9076d060199e4099748f0a39> | ||
| - やっぱり例外処理派かぁ | ||
|
|
||
| ## 5. ステップ3 | ||
|
|
||
| ステップ2と同じ. | ||
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,10 @@ | ||
| #!/usr/bin/bash | ||
| set -eo pipefail | ||
|
|
||
| main (){ | ||
| local problem | ||
| problem="$*" | ||
| problem="${problem//[[:space:]]/_}" | ||
| cp -r templates "$problem" | ||
| } | ||
| main "$@" |
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,49 @@ | ||
| <!-- PR のタイトルには, LeetCode の問題タイトルを含める.--> | ||
| <!-- レビュー依頼定型文 | ||
| [問題とそのリンク]を解きました. | ||
| [Pull Request の URL] | ||
| レビューをお願いします. | ||
| --> | ||
| # [Problem TitLe] <!-- omit in toc --> | ||
|
|
||
| ## 1. 問題 | ||
|
|
||
| ### 1.1. リンク | ||
|
|
||
| <> | ||
|
|
||
| ### 1.2. 問題概要 (閲覧制限のある問題の場合のみ) | ||
|
|
||
| ## 2. 次に取り組む問題のリンク | ||
|
|
||
| <> | ||
|
|
||
| ## 3. ステップ1 | ||
|
|
||
| ### 3.1. コード | ||
|
|
||
| ```python | ||
|
|
||
| ``` | ||
|
|
||
| ### 3.2. 時間・空間計算量 | ||
|
|
||
| ## 4. ステップ2 | ||
|
|
||
| ### 4.1. コード | ||
|
|
||
| ```python | ||
|
|
||
| ``` | ||
|
|
||
| ### 4.2. 講師陣のコメントとして想定されること | ||
|
|
||
| ### 4.3. 他の人のコードを読んで考えたこと | ||
|
|
||
| ### 4.4. 改善するときに考えたこと | ||
|
|
||
| ## 5. ステップ3 | ||
|
|
||
| ```python | ||
|
|
||
| ``` |
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.
あまり自然言語処理以外のところでtoを2にするのは見ない気がするので、toにしたほうがわかりやすい気がします。(Nand2Tetrisとかはあるので私の感覚がズレてるかもですが)
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.
to を 2 にしたり、 for を 4 にするのは、十年以上前のハッカー文化を感じます。最近はあまり見かけないように思います。