-
Notifications
You must be signed in to change notification settings - Fork 0
Create 1.TwoSum.md #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?
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,77 @@ | ||
| # 1st | ||
| - 問題: [1. Two Sum](https://leetcode.com/problems/two-sum/) | ||
| - コメント集: [1. Two Sum](https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/mobilebasic#h.7b7phcxky0ug) | ||
| - [身体性を持った考え方があるよね](https://discord.com/channels/1084280443945353267/1237649827240742942/1249892025948573706) | ||
| - [1000枚のカードが配られて、2枚のカードの和がある数になる時、2枚のカードをどう探しますか?](https://discord.com/channels/1084280443945353267/1229085360403775569/1229955174446137344) | ||
| - [類似コメント](https://discord.com/channels/1084280443945353267/1218823752243089408/1239214204742013009) | ||
| - なるほど、これはその通りだし、解法が自然に思いつくと感じた | ||
| - こういう言い換えを頭の中で行なって解法を組み立て、相手に説明すればいいな | ||
| - [Exceptionの話1](https://discord.com/channels/1084280443945353267/1263078966491877377/1296874739377115243) | ||
| - [Exceptionの話2](https://discord.com/channels/1084280443945353267/1346189624414048360/1346775042322862152) | ||
| - [データ構造の名称を変数名として使うなの話](http://discord.com/channels/1084280443945353267/1337068598337736838/1338007445892763732) | ||
| - これ自分も最初に書く時にやりがちなので反省 | ||
| - 「例えば、部屋という抽象的な単語は文脈に応じて具体的な名前(会議室、食堂)で呼ぶでしょ?」という話 | ||
| - 条件 | ||
| - `2 <= nums.length <= 10^4` | ||
| - `-10^9 <= nums[i] <= 10^9` | ||
| - `-10^9 <= target <= 10^9` | ||
| - 方針 | ||
| - 数字2つのペアの和が必ず target を満たすので、一回のループを回しながら target - nums[i] を hashmap に記録していく | ||
| - nums[i]以降を走査する時、すでに map に nums[i以降]がkeyとしてあれば、mapに記録しているインデックスと今走査しているインデックスの組み合わせが答えとなる | ||
| - 時間計算量: O(N) | ||
| - 空間計算量: O(N) | ||
| - 時間 | ||
| - 10分 | ||
|
|
||
| ```java | ||
| class Solution { | ||
| public int[] twoSum(int[] nums, int target) { | ||
| HashMap<Integer, Integer> map = new HashMap<>(); | ||
| for (int i = 0; i < nums.length; i++) { | ||
| if (map.containsKey(nums[i])) { | ||
| return new int[]{map.get(nums[i]), i}; | ||
| } | ||
| int temp = target - nums[i]; | ||
| map.put(temp, i); | ||
| } | ||
| return null; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## 2nd | ||
| - 変数名を綺麗にする | ||
| - null を返していたが、Exception が適切だと判断した | ||
|
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がメソッドのシグニチャを決めてるので出来ませんが)例外を投げるよりかはOptional<int[]> を返り値の型にする方が好ましいと思います。解があるかどうかを呼び出し側がちゃんと検査するコードにすることを型システムでサポートできるので。 |
||
| ```java | ||
| class Solution { | ||
| public int[] twoSum(int[] nums, int target) { | ||
| HashMap<Integer, Integer> complementToIndex = new HashMap<>(); | ||
| for (int i = 0; i < nums.length; i++) { | ||
| if (complementToIndex.containsKey(nums[i])) { | ||
| return new int[]{complementToIndex.get(nums[i]), i}; | ||
| } | ||
| int complement = target - nums[i]; | ||
| complementToIndex.put(complement, i); | ||
| } | ||
| throw new IllegalArgumentException("numsに含まれる要素のペアで、和がtargetを満たすものは存在しない"); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## 3rd | ||
| - 3回かく | ||
| ```java | ||
| class Solution { | ||
| public int[] twoSum(int[] nums, int target) { | ||
| HashMap<Integer, Integer> complementToIndex = new 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. やや好みの問題ですが、「補数とそいつを補数とするような要素のインデックス」はちょっと自然言語で説明し辛にくく思います。説明の順番と意味上の主語が出る順番がぎゃくなので。その観点では自然言語で説明しやすく思える「要素の値とそのインデックス」の方が好ましいと思います(numToIndex とか)。 何で自然言語での説明のしやすさを優先したいかというと、説明しやすい処理ほど読み手が読みやすい(=理解するのにリソースを割かなくて済む)からですね |
||
| for (int i = 0; i < nums.length; i++) { | ||
| if (complementToIndex.containsKey(nums[i])) { | ||
| return new int[]{complementToIndex.get(nums[i]), i}; | ||
| } | ||
| int complement = target - nums[i]; | ||
| complementToIndex.put(complement, i); | ||
| } | ||
| throw new IllegalArgumentException("numsに含まれる要素のペアで、和がtargetを満たすものは存在しない"); | ||
|
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. 今回の例外ならエラーメッセージは英語で書くんじゃないかなぁと思います。文字化けの心配が少ないし、読める人が多いし。 日本語を扱うユーザに直接見せるエラーメッセージなら日本語を直接書くのも選択肢の一つではありますが、割とそれも弱めな選択肢かなぁ、と思います。エラーメッセージ自体は事前に定義しておいた英数字からなるIDなり通し番号をつけておいて、UIに表示させるときに対応する日本語のエラーメッセージに置き換える、みたいな実装をしても良いわけですし。 |
||
| } | ||
| } | ||
| ``` | ||
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.
と、操作を行うための最小限のインターフェースで受けるコードをよく目にします。このあたりは、所属するチームの一般的な書き方に合わせることをおすすめします。