From 4cbafe39511752cc9076d060199e4099748f0a39 Mon Sep 17 00:00:00 2001 From: mt2324 <63892273+mt2324@users.noreply.github.com> Date: Thu, 19 Feb 2026 17:56:35 +0900 Subject: [PATCH] Add files via upload --- 1. Two Sum.md | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 1. Two Sum.md diff --git a/1. Two Sum.md b/1. Two Sum.md new file mode 100644 index 0000000..711e7ef --- /dev/null +++ b/1. Two Sum.md @@ -0,0 +1,93 @@ +https://leetcode.com/problems/two-sum/ + +### Description + +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. + +**Example 1:** + +**Input:** nums = [2,7,11,15], target = 9 +**Output:** [0,1] +**Explanation:** Because nums[0] + nums[1] == 9, we return [0, 1]. + +**Example 2:** + +**Input:** nums = [3,2,4], target = 6 +**Output:** [1,2] + +**Example 3:** + +**Input:** nums = [3,3], target = 6 +**Output:** [0,1] + +**Constraints:** + +- `2 <= nums.length <= 104` +- `-109 <= nums[i] <= 109` +- `-109 <= target <= 109` +- **Only one valid answer exists.** + +**Follow-up:** Can you come up with an algorithm that is less than `O(n2)` time complexity? +### STEP1 +brute forceしか思いつかなかったので解答をみた。 +nをnumsの長さとして +Time ComplexityO(n) Space ComplexityO(n) +```python +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + d = {} + for i, x in enumerate(nums): + d[x] = i + for i, x in enumerate(nums): + if target - x in d and i != d[target - x]: + return [i,d[target-x]] +``` + +### STEP2 +あまりに辞書の変数が酷いけどどうしたもんかなと思っていたら +https://github.com/ksaito0629/leetcode_arai60/pull/10/changes/bb1ae591a98e7810711de49f212bb97f5fc34ce9#r2811748134 +辞書の名前としてkey_to_valueはなるほど感がある。 +というか2周ループを回す必要は別になくて探しながら追加したらいいだけだな。 +あとleetcodeで書くときにエラーをraiseする習慣はなかったけど他の人のコードを見ていると書いているっぽい。仕事なら書いてるけども。 + +```python +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + num_to_index = {} + for index, num in enumerate(nums): + if target - num in num_to_index: + return [index, num_to_index[target-num]] + num_to_index[num] = index + raise NoValueError("There is no such pair that sums to the target") +``` +brute forceも一応書いておく。 +Time ComplexityO(n^2) Space ComplexityO(1) +```python +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + length = len(nums) + for i in range(length): + for j in range(i+1, length): + if nums[i] + nums[j] == target: + return [i,j] + raise NoValueError("There is no such pair that sums to the target.") +``` + +### STEP3 +```python +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + num_to_index = {} + for index, num in enumerate(nums): + if target - num in num_to_index: + return [index, num_to_index[target-num]] + num_to_index[num] = index + raise NoValueError("There is no such pair that sums to the target") +``` + +そういえばpythonのエラーってどんな感じだったっけと思って見直した +https://docs.python.org/3.15/tutorial/errors.html