From 48c0e4b60de7ff449dfaa00c1f658c990ce3abe4 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Sat, 4 Apr 2026 06:35:20 +0000 Subject: [PATCH 1/7] docs: add memo.md template for new branch --- memo.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 memo.md diff --git a/memo.md b/memo.md new file mode 100644 index 0000000..4aa1f71 --- /dev/null +++ b/memo.md @@ -0,0 +1,12 @@ +## Step 1 +```python + +``` +## Step 2 +```python + +``` +## Step 3 +```python + +``` \ No newline at end of file From 4ca9e34ccce90be22362dc433bc1f82c1d865c55 Mon Sep 17 00:00:00 2001 From: fumi1326 Date: Sat, 4 Apr 2026 15:57:03 +0900 Subject: [PATCH 2/7] solution to 1.Two sum --- memo.md | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/memo.md b/memo.md index 4aa1f71..567a25d 100644 --- a/memo.md +++ b/memo.md @@ -1,9 +1,29 @@ ## Step 1 +先頭から順番に各要素について後ろにある各要素を足してtargetと比較するやり方が一番直感的に思いついたので書いてみる。各要素について線形の走査が入るので時間計算量がO(n^2)になる。想定解法ではないが一回書いてみる。 ```python - +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + for i in range(len(nums)): + for j in range(i + 1, len(nums)): + if nums[i] + nums[j] == target: + answer = [i, j] + break + return answer ``` ## Step 2 +先頭の要素から順番に、targetとの差分を計算して、その差分の値がそれより前にあればそのindexを持ってくるやり方であれば、1回の走査とハッシュマップへのアクセスだけで済むので、時間計算量はO(n)になる。一方でハッシュマップを置いておくメモリが必要で、そのメモリはnumsの要素数に対して比例になるため、空間計算量もO(n)になる。 ```python +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + visited = {} + for i in range(len(nums)): + diff = target - nums[i] + if diff in visited: + answer = [visited[diff], i] + break + else: + visited[nums[i]] = i + return answer ``` ## Step 3 From c86d229388a06416bdd12b6d09c710cd4708e4dd Mon Sep 17 00:00:00 2001 From: fumi1326 Date: Sat, 4 Apr 2026 16:00:27 +0900 Subject: [PATCH 3/7] =?UTF-8?q?Step=202=E3=81=BE=E3=81=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- memo.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/memo.md b/memo.md index 567a25d..7fb6dce 100644 --- a/memo.md +++ b/memo.md @@ -1,3 +1,6 @@ +問題は1. Two Sum(https://leetcode.com/problems/two-sum/description/?envType=problem-list-v2&envId=xo2bgr0r) +次に解くのは387. First Unique Character in a String(https://leetcode.com/problems/first-unique-character-in-a-string/?envType=problem-list-v2&envId=xo2bgr0r) + ## Step 1 先頭から順番に各要素について後ろにある各要素を足してtargetと比較するやり方が一番直感的に思いついたので書いてみる。各要素について線形の走査が入るので時間計算量がO(n^2)になる。想定解法ではないが一回書いてみる。 ```python @@ -25,8 +28,4 @@ class Solution: visited[nums[i]] = i return answer -``` -## Step 3 -```python - ``` \ No newline at end of file From 45c494e2b1d386a1cb51b33d8ebf67a2b8b89d03 Mon Sep 17 00:00:00 2001 From: fumi1326 Date: Sat, 4 Apr 2026 16:03:05 +0900 Subject: [PATCH 4/7] =?UTF-8?q?=E5=90=84=E5=95=8F=E9=A1=8C=E7=94=A8?= =?UTF-8?q?=E3=81=AE=E3=83=87=E3=82=A3=E3=83=AC=E3=82=AF=E3=83=88=E3=83=AA?= =?UTF-8?q?=E3=82=92=E4=BD=9C=E3=81=A3=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- memo.md => 1. Two Sum/memo.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename memo.md => 1. Two Sum/memo.md (100%) diff --git a/memo.md b/1. Two Sum/memo.md similarity index 100% rename from memo.md rename to 1. Two Sum/memo.md From 1df0aa795fed60fc1c1419acd0636c7a6b66b58a Mon Sep 17 00:00:00 2001 From: fumi1326 Date: Sat, 4 Apr 2026 16:04:50 +0900 Subject: [PATCH 5/7] fix: add missing line break in memo.md for better formatting --- 1. Two Sum/memo.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1. Two Sum/memo.md b/1. Two Sum/memo.md index 7fb6dce..3904885 100644 --- a/1. Two Sum/memo.md +++ b/1. Two Sum/memo.md @@ -1,4 +1,4 @@ -問題は1. Two Sum(https://leetcode.com/problems/two-sum/description/?envType=problem-list-v2&envId=xo2bgr0r) +問題は1. Two Sum(https://leetcode.com/problems/two-sum/description/?envType=problem-list-v2&envId=xo2bgr0r)\\ 次に解くのは387. First Unique Character in a String(https://leetcode.com/problems/first-unique-character-in-a-string/?envType=problem-list-v2&envId=xo2bgr0r) ## Step 1 From d0ac9edfe539ea1b0ffd8d0be224815eae4978aa Mon Sep 17 00:00:00 2001 From: fumi1326 Date: Sat, 4 Apr 2026 16:06:58 +0900 Subject: [PATCH 6/7] fix: add missing line break in memo.md for better formatting --- 1. Two Sum/memo.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/1. Two Sum/memo.md b/1. Two Sum/memo.md index 3904885..d2e3217 100644 --- a/1. Two Sum/memo.md +++ b/1. Two Sum/memo.md @@ -1,4 +1,5 @@ -問題は1. Two Sum(https://leetcode.com/problems/two-sum/description/?envType=problem-list-v2&envId=xo2bgr0r)\\ +問題は1. Two Sum(https://leetcode.com/problems/two-sum/description/?envType=problem-list-v2&envId=xo2bgr0r) + 次に解くのは387. First Unique Character in a String(https://leetcode.com/problems/first-unique-character-in-a-string/?envType=problem-list-v2&envId=xo2bgr0r) ## Step 1 From bfa2644a64d054db2a8cef0faef5acabe89e1312 Mon Sep 17 00:00:00 2001 From: fumi1326 Date: Mon, 6 Apr 2026 15:32:46 +0900 Subject: [PATCH 7/7] feat: add Step 3 implementation for Two Sum solution based on feedback --- 1. Two Sum/memo.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/1. Two Sum/memo.md b/1. Two Sum/memo.md index d2e3217..8b5c6bd 100644 --- a/1. Two Sum/memo.md +++ b/1. Two Sum/memo.md @@ -29,4 +29,19 @@ class Solution: visited[nums[i]] = i return answer +``` + +## Step3 +これまでもらった指摘を基に書き直してみる。 +```Python +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + num_to_index = {} + for i, num in enumerate(nums): + complement = target - num + if complement in num_to_index: + return [num_to_index[complement], i] + else: + num_to_index[num] = i + ``` \ No newline at end of file