From 3c5e6a55a59435cfe2f4cb2a6cd82339037a73f2 Mon Sep 17 00:00:00 2001 From: busker <165013324+hiroki-horiguchi-dev@users.noreply.github.com> Date: Sat, 21 Mar 2026 19:35:52 +0900 Subject: [PATCH 1/3] Create 1.md --- hashmap/1.md | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 hashmap/1.md diff --git a/hashmap/1.md b/hashmap/1.md new file mode 100644 index 0000000..80bdcb2 --- /dev/null +++ b/hashmap/1.md @@ -0,0 +1,69 @@ +# 1st +- 問題: [1. Two Sum](https://leetcode.com/problems/two-sum/) +- コメント集: [1. Two Sum](https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/mobilebasic#h.7b7phcxky0ug) +- 条件 + - `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 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 が適切だと判断した +```java +import java.rmi.UnexpectedException; + +class Solution { + public int[] twoSum(int[] nums, int target) { + HashMap 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 UnexpectedException("numsに含まれる要素のペアで、和がtargetを満たすものは存在しない"); + } +} +``` + +## 3rd +- 3回かく +```java +class Solution { + public int[] twoSum(int[] nums, int target) { + HashMap 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 UnexpectedException("numsに含まれる要素のペアで、和がtargetを満たすものは存在しない"); + } +} +``` \ No newline at end of file From 0f3f7ce675f0b7de1ecf538fce3c3b20033ddd10 Mon Sep 17 00:00:00 2001 From: busker <165013324+hiroki-horiguchi-dev@users.noreply.github.com> Date: Sun, 22 Mar 2026 12:14:49 +0900 Subject: [PATCH 2/3] =?UTF-8?q?=E6=8A=95=E3=81=92=E3=82=8B=E3=81=B9?= =?UTF-8?q?=E3=81=8D=E4=BE=8B=E5=A4=96=E3=82=92=E9=96=93=E9=81=95=E3=81=88?= =?UTF-8?q?=E3=81=A6=E3=81=84=E3=81=9F=E3=81=AE=E3=81=A7=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hashmap/1.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/hashmap/1.md b/hashmap/1.md index 80bdcb2..a857906 100644 --- a/hashmap/1.md +++ b/hashmap/1.md @@ -33,8 +33,6 @@ class Solution { - 変数名を綺麗にする - null を返していたが、Exception が適切だと判断した ```java -import java.rmi.UnexpectedException; - class Solution { public int[] twoSum(int[] nums, int target) { HashMap complementToIndex = new HashMap<>(); @@ -45,7 +43,7 @@ class Solution { int complement = target - nums[i]; complementToIndex.put(complement, i); } - throw new UnexpectedException("numsに含まれる要素のペアで、和がtargetを満たすものは存在しない"); + throw new IllegalArgumentException("numsに含まれる要素のペアで、和がtargetを満たすものは存在しない"); } } ``` @@ -63,7 +61,7 @@ class Solution { int complement = target - nums[i]; complementToIndex.put(complement, i); } - throw new UnexpectedException("numsに含まれる要素のペアで、和がtargetを満たすものは存在しない"); + throw new IllegalArgumentException("numsに含まれる要素のペアで、和がtargetを満たすものは存在しない"); } } ``` \ No newline at end of file From 59cfab3db467eec0f84fbb06068e3dd985e61a0f Mon Sep 17 00:00:00 2001 From: busker <165013324+hiroki-horiguchi-dev@users.noreply.github.com> Date: Sun, 22 Mar 2026 12:21:23 +0900 Subject: [PATCH 3/3] =?UTF-8?q?=E3=82=B3=E3=83=A1=E3=83=B3=E3=83=88?= =?UTF-8?q?=E3=82=92=E8=AA=AD=E3=82=93=E3=81=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hashmap/1.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/hashmap/1.md b/hashmap/1.md index a857906..9081546 100644 --- a/hashmap/1.md +++ b/hashmap/1.md @@ -1,6 +1,16 @@ # 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`